Jump to content

Recommended Posts

Hello, can anyone help me or explain how to make my character fly? I’m just looking and can’t find an answer to my question and how to add wings on which the character will fly since he is a butterfly... sorry for my broken English, I’m translating everything from a translator and I was really afraid to ask about it...

and if there is no opportunity to give him this ability, then I’m sorry for wasting your time...

Hello, I have some suggestions for character flight. While I do not know of any flying characters, you can add code that would allow your character to jump far distances, such as across rivers and crevasses. The code I'd recommend using is from the fixed version of  Frog Webber. You'll need to pull some assets such as the leapstaff component and animations for player_leap. They're found in DST's Mods folder, which is found by right-clicking DST on Steam, choosing "Manage" ---> "Browse Local Files", and finding:

mods\workshop-2249540093\scripts\components

The player_leap.zip file found in:

mods\workshop-2249540093\anim

I will paste the code I use personally to create jumping characters. It is edited so that it no longer requires a Walking Cane to use. You will have to paste the character's name over wherever I type "CHARACTER_NAME".

Start by adding this tag into your local common_postinit found in your character's prefab file.

inst:AddTag ("CHARACTER_NAME")

Next you will post the following into your character's modmain.lua file. Please remember to replace any instances of "CHARACTER_NAME". If you are editing using Notepad++, you can press Ctrl + F to quickly check for all instances of "CHARACTER_NAME", although there's only one near the top. Also, remember to keep the quotation marks around the name.
 

Spoiler
local LEAP_MAX_DIST = 10
local LEAP_MIN_DIST = 3
local LEAP_MAX_SPEED = 16
local LEAP_MIN_SPEED = LEAP_MAX_SPEED * LEAP_MIN_DIST / LEAP_MAX_DIST
local LEAP_MAX_DIST_SQ = LEAP_MAX_DIST * LEAP_MAX_DIST
local LEAP_MIN_DIST_SQ = LEAP_MIN_DIST * LEAP_MIN_DIST

local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS
local ACTIONS = GLOBAL.ACTIONS
local FRAMES = GLOBAL.FRAMES
local DEGREES = GLOBAL.DEGREES
local COLLISION = GLOBAL.COLLISION
local TheSim = GLOBAL.TheSim
local State = GLOBAL.State
local EventHandler = GLOBAL.EventHandler
local TimeEvent = GLOBAL.TimeEvent
local ActionHandler = GLOBAL.ActionHandler
local Vector3 = GLOBAL.Vector3
local PlayFootstep = GLOBAL.PlayFootstep
local SpawnPrefab = GLOBAL.SpawnPrefab

----------------------------------------------------------------------------------------------------------------

local leap_action = AddAction("LEAP", "leap", function(act)
    if act.doer ~= nil and act.doer.sg ~= nil and act.doer.sg.currentstate.name == "leap_pre" then
        act.doer.sg:GoToState("leap", { pos = act.pos })
        return true
    end
end)

leap_action.priority = 10
leap_action.rmb = true
leap_action.distance = 1024

AddComponentPostInit("playeractionpicker", function(self)
    if self.inst:HasTag("CHARACTER_NAME") then
        local LEAPACTION = GLOBAL.ACTIONS.LEAP
        local _GetRightClickActions = self.GetRightClickActions
        self.GetRightClickActions = function(self, position, target)
            local actions = _GetRightClickActions(self, position, target)
            if #actions == 0 then
                local ishungry = self.inst.replica.hunger:GetCurrent() < 10
                local ispassable = self.map:IsPassableAtPoint(position:Get())
                if ispassable and not ishungry then
                    actions = self:SortActionList({LEAPACTION}, position)
                end
            end
            return actions
        end
    end
end)

--------------------------------------------------------------------------

AddStategraphState("wilson", State{
    name = "leap_pre",
    tags = { "doing", "busy", "canrotate" },

    onenter = function(inst)
        inst.components.locomotor:Stop()
        inst.AnimState:PlayAnimation("leap_pre")
    end,

    events =
    {
        EventHandler("animover", function(inst)
            if inst.AnimState:AnimDone() then
                if inst.bufferedaction ~= nil then
                    inst:PerformBufferedAction()
                else
                    inst.sg:GoToState("idle")
                end
            end
        end),
    },
})

local function ToggleOffPhysics(inst)
    inst.sg.statemem.isphysicstoggle = true
    inst.Physics:ClearCollisionMask()
    inst.Physics:CollidesWith(COLLISION.GROUND)
end

local function ToggleOnPhysics(inst)
    inst.sg.statemem.isphysicstoggle = nil
    inst.Physics:ClearCollisionMask()
    inst.Physics:CollidesWith(COLLISION.WORLD)
    inst.Physics:CollidesWith(COLLISION.OBSTACLES)
    inst.Physics:CollidesWith(COLLISION.SMALLOBSTACLES)
    inst.Physics:CollidesWith(COLLISION.CHARACTERS)
    inst.Physics:CollidesWith(COLLISION.GIANTS)
end

AddStategraphState("wilson", State{
    name = "leap",
    tags = { "doing", "busy", "canrotate", "nopredict", "nomorph" },

    onenter = function(inst, data)
        inst.components.locomotor:Stop()
        inst.AnimState:PlayAnimation("leap")

        local dist
        if data ~= nil and data.pos ~= nil then
            
                     local pos = data.pos:GetPosition()
                     inst:ForceFacePoint(pos.x, 0, pos.z)
        
            local distsq = inst:GetDistanceSqToPoint(pos)
            if distsq <= LEAP_MIN_DIST_SQ then
                dist = LEAP_MIN_DIST
                inst.sg.statemem.speed = LEAP_MIN_SPEED
            elseif distsq >= LEAP_MAX_DIST_SQ then
                dist = LEAP_MAX_DIST
                inst.sg.statemem.speed = LEAP_MAX_SPEED
            else
                dist = math.sqrt(distsq)
                inst.sg.statemem.speed = LEAP_MAX_SPEED * dist / LEAP_MAX_DIST
            end
        else
            inst.sg.statemem.speed = LEAP_MAX_SPEED
            dist = LEAP_MAX_DIST
        end

        if inst.components.hunger ~= nil then
            inst.components.hunger:DoDelta(-dist / LEAP_MAX_DIST, true)
        end

        local x, y, z = inst.Transform:GetWorldPosition()
        local angle = inst.Transform:GetRotation() * DEGREES
        if GLOBAL.TheWorld.Map:IsPassableAtPoint(x + dist * math.cos(angle), 0, z - dist * math.sin(angle)) then
            ToggleOffPhysics(inst)
        end

        inst.Physics:SetMotorVel(inst.sg.statemem.speed * .5, 0, 0)

        local fx = SpawnPrefab("small_puff")
        if fx ~= nil then
            fx.Transform:SetScale(.3, .3, .3)
            fx.Transform:SetPosition(x, 0, z)
        end
        PlayFootstep(inst)

        if inst.components.playercontroller ~= nil then
            inst.components.playercontroller:Enable(false)
        end
    end,

    onupdate = function(inst)
        if inst.sg.statemem.isphysicstoggle then
            local x, y, z = inst.Transform:GetWorldPosition()
            local angle = inst.Transform:GetRotation() * DEGREES
            local radius = .5
            x = x + .75 * radius * math.cos(angle)
            z = z - .75 * radius * math.sin(angle)
            local ents = TheSim:FindEntities(x, 0, z, radius, { "wall" })
            for i, v in ipairs(ents) do
                if v.components.health ~= nil and v.components.health:GetPercent() > .5 then
                    ToggleOnPhysics(inst)
                    return
                end
            end
        end
    end,

    timeline =
    {
        TimeEvent(.5 * FRAMES, function(inst)
            inst.Physics:SetMotorVel(inst.sg.statemem.speed * .75, 0, 0)
        end),
        TimeEvent(1 * FRAMES, function(inst)
            inst.Physics:SetMotorVel(inst.sg.statemem.speed, 0, 0)
        end),
        TimeEvent(19 * FRAMES, function(inst)
            inst.Physics:SetMotorVel(inst.sg.statemem.speed * .25, 0, 0)
            inst.SoundEmitter:PlaySound("dontstarve/movement/bodyfall_dirt")

            local fx = SpawnPrefab("small_puff")
            if fx ~= nil then
                fx.Transform:SetScale(.3, .3, .3)
                local x, y, z = inst.Transform:GetWorldPosition()
                local angle = inst.Transform:GetRotation() * DEGREES
                fx.Transform:SetPosition(x + .25 * math.cos(angle), 0, z - .25 * math.sin(angle))
            end
        end),
        TimeEvent(20 * FRAMES, function(inst)
            inst.Physics:Stop()
            inst.sg:GoToState("idle", true)
        end),
    },

    onexit = function(inst)
        if inst.sg.statemem.isphysicstoggle then
            ToggleOnPhysics(inst)
        end

        if inst.components.playercontroller ~= nil then
            inst.components.playercontroller:Enable(true)
        end
    end,
})

AddStategraphState("wilson_client", State{
    name = "leap_pre",
    tags = { "doing", "busy", "canrotate" },

    onenter = function(inst)
        inst.components.locomotor:Stop()
        inst.AnimState:PlayAnimation("leap_pre")
        inst.AnimState:PushAnimation("leap_lag", false)

        inst:PerformPreviewBufferedAction()
        inst.sg:SetTimeout(2)
    end,

    onupdate = function(inst)
        if inst:HasTag("doing") then
            if inst.entity:FlattenMovementPrediction() then
                inst.sg:GoToState("idle", "noanim")
            end
        elseif inst.bufferedaction == nil then
            inst.sg:GoToState("idle")
        end
    end,

    ontimeout = function(inst)
        inst:ClearBufferedAction()
        inst.sg:GoToState("idle")
    end,
})

AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.LEAP, function(inst)
    return not inst.sg:HasStateTag("busy")
        and (inst.sg:HasStateTag("moving") or inst.sg:HasStateTag("idle"))
        and "leap_pre"
        or nil
end))

AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.LEAP, function(inst)
    return not (inst.sg:HasStateTag("busy") or inst:HasTag("busy"))
        and inst.entity:CanPredictMovement()
        and (inst.sg:HasStateTag("moving") or inst.sg:HasStateTag("idle"))
        and "leap_pre"
        or nil
end))

--------------------------------------------------------------------------

local function ReticuleTargetFn()
    local player = GLOBAL.ThePlayer
    local ground = GLOBAL.TheWorld.Map
    local x, y, z
    for r = LEAP_MAX_DIST * .6, LEAP_MAX_DIST, .25 do
        x, y, z = player.entity:LocalToWorldSpace(r, 0, 0)
        if ground:IsPassableAtPoint(x, y, z) then
            return Vector3(x, y, z)
        end
    end
    for r = LEAP_MAX_DIST * .6 - .25, LEAP_MIN_DIST, -.25 do
        x, y, z = player.entity:LocalToWorldSpace(r, 0, 0)
        if ground:IsPassableAtPoint(x, y, z) then
            return Vector3(x, y, z)
        end
    end
    return Vector3(x, y, z)
end

 

I hope you find this useful! If this doesn't satisfy you, then I do apologize. The best I can offer other than this would be to attempt to reverse-engineer code from Gleenus' Airplane Mod. If there are any translation errors, please reply on this thread so that a native speaker may help translate in your native language.

@ViridianCrown
small problem, it basically works but the character has disappeared... 

image.thumb.png.fda2253d32656f9edbf30c608e464cdf.png

A! everything worked, I realized what I did wrong
but another problem arose it doesn’t jump....

Edited by Ileana_Saphira
1 hour ago, Ileana_Saphira said:

@ViridianCrown
small problem, it basically works but the character has disappeared... 

image.thumb.png.fda2253d32656f9edbf30c608e464cdf.png

A! everything worked, I realized what I did wrong
but another problem arose it doesn’t jump....

Ah! I completely forgot! You need to set a destination for the player_jump animation file in the character's preset file. Just go over to local assets at the top of your character's lua file and place this line between the parenthesis, preferably directly under any other files:

Asset("ANIM", "anim/player_leap.zip"),

Best of luck with the rest of the character!

ViridianCrown

I did something wrong... I did something wrong... I even changed the player to the character name, for some reason an error appears...

_2023-09-14_021321131.thumb.png.75683d7b71d4f907149489fb55ff0912.png_2023-09-14_021734876.thumb.png.76eb8d51d8c886be3856ee4af960e0a0.png

Edited by Ileana_Saphira

Well, with the player_leap it keeps that name as the code searches specifically within the player_leap folder, so renaming it would break it. Hope this helps!

So it should look:

local MakePlayerCharacter = require "prefabs/player_common"

local assets= {
Asset( "SCRIPT", "scripts/prefabs/player_common.lua" )
Asset( "ANIM", "anim/charax.zip" )
Asset( "ANIM", "anim/ghost_charax_build.zip" ),
Asset( "ANIM", "anim/player_leap.zip" ),
}

 

20 minutes ago, Ileana_Saphira said:

ViridianCrown

I did something wrong... I did something wrong... I even changed the player to the character name, for some reason an error appears...

_2023-09-14_021321131.thumb.png.75683d7b71d4f907149489fb55ff0912.png_2023-09-14_021734876.thumb.png.76eb8d51d8c886be3856ee4af960e0a0.png

 

Interesting... Look inside your character's anim folder. Is there a .zip there named player_leap.zip? If not, then you'll have to insert this file into said folder, as this contains all the necessary animation data for leaping.player_leap.zip

Edited by ViridianCrown

I think I've found the problem. Where I wrote the phrase "CHARACTER_NAME" as a placeholder, I meant replacing the entire phrase, not just the "NAME" component. you the code was searching incorrectly for a character named "CHARACTER_CHARAX" when the name it should be searching for is "charax". I'm sending back the files with the fixed phrase. Hope it works this time! prefab.zip

ViridianCrown

I changed it, it appeared, but the problem is the same, it doesn’t jump...

 

and excuse me, I probably don’t understand something, because I’m sleepy...

Edited by Ileana_Saphira

If you could send me all the character files (including animations and images so I can test in-game), I could take a look for you at everything and see if there's any problems pertaining to file placement or missing files I could replace. I can send it back once I'm done and could maybe have it done by the time you wake up.

ViridianCrown

I hope I can trust you with it
but I'm a little afraid to post this
I’m just afraid that someone will misappropriate this, although I haven’t encountered this yet

 

Edited by Ileana_Saphira

Good news! I looked over the code, tweaked some parts, added some missing bits and now he leaps! I'll send over the file now, just know that I'll be editing my messages to remove all the links so that nobody else can misuse them as you're worried about. Just respond to this telling me you've received it and I'll take down the download link. The problem appeared to be that certain GLOBAL. parts weren't specified:

local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS
local Recipe = GLOBAL.Recipe
local RECIPETABS = GLOBAL.RECIPETABS
local TECH = GLOBAL.TECH

With these added, the code functions as intended. Best of luck with the character! Great character art, by the way!

 

Edited by ViridianCrown

ViridianCrown

hee thank you very much! for your help and I’m even glad that you liked my Art :3
and I'm even glad that I can trust you
and he jumps!
Hooray!!! (◕ヮ◕ )

Edited by Ileana_Saphira
On 9/14/2023 at 2:47 AM, ViridianCrown said:

Good news! I looked over the code, tweaked some parts, added some missing bits and now he leaps! I'll send over the file now, just know that I'll be editing my messages to remove all the links so that nobody else can misuse them as you're worried about. Just respond to this telling me you've received it and I'll take down the download link. The problem appeared to be that certain GLOBAL. parts weren't specified:

local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS
local Recipe = GLOBAL.Recipe
local RECIPETABS = GLOBAL.RECIPETABS
local TECH = GLOBAL.TECH

With these added, the code functions as intended. Best of luck with the character! Great character art, by the way!

 

Sorry for reviving old thread, but how to change amount of hunger taken away when jumping? Whatever I do it's taking away only 1 hunger point

And also this code crashes when equipping lazy explorer:

[string "scripts/componentactions.lua"]:2632: attempt to index a nil value
LUA ERROR stack traceback:
    scripts/componentactions.lua:2632 in (method) UnregisterComponentActions (Lua) <2617-2644>
    scripts/entityscript.lua:655 in (method) RemoveComponent (Lua) <642-657>
    scripts/components/aoetargeting.lua:107 in (method) StopTargeting (Lua) <105-112>
    scripts/components/playercontroller.lua:241 in (local) fn (Lua) <229-258>
    scripts/entityscript.lua:1208 in (method) PushEvent (Lua) <1195-1222>
    scripts/prefabs/inventory_classified.lua:423 in (field) fn (Lua) <416-427>
    scripts/scheduler.lua:186 in (method) OnTick (Lua) <164-216>
    scripts/scheduler.lua:419 in (global) RunStaticScheduler (Lua) <417-425>
    scripts/update.lua:178 in () ? (Lua) <169-220>

 

On 8/31/2024 at 9:42 AM, RexySeven said:

Sorry for reviving old thread, but how to change amount of hunger taken away when jumping? Whatever I do it's taking away only 1 hunger point

And also this code crashes when equipping lazy explorer:

[string "scripts/componentactions.lua"]:2632: attempt to index a nil value
LUA ERROR stack traceback:
    scripts/componentactions.lua:2632 in (method) UnregisterComponentActions (Lua) <2617-2644>
    scripts/entityscript.lua:655 in (method) RemoveComponent (Lua) <642-657>
    scripts/components/aoetargeting.lua:107 in (method) StopTargeting (Lua) <105-112>
    scripts/components/playercontroller.lua:241 in (local) fn (Lua) <229-258>
    scripts/entityscript.lua:1208 in (method) PushEvent (Lua) <1195-1222>
    scripts/prefabs/inventory_classified.lua:423 in (field) fn (Lua) <416-427>
    scripts/scheduler.lua:186 in (method) OnTick (Lua) <164-216>
    scripts/scheduler.lua:419 in (global) RunStaticScheduler (Lua) <417-425>
    scripts/update.lua:178 in () ? (Lua) <169-220>

 

I'm not a modder myself but maybe you can try look through the Whisky mod, She got the ability to leap in the cost of hunger and it's a fairly recent mod.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...