Jump to content

Recommended Posts

I'm new to this and my English is not good, I'll be direct

i'm trying to make my character when running increase her speed by passing a few seconds making it faster if you continue.


My problem is how can I detect that its status is running and it is not still standing 

At the moment I am using this code

Spoiler
local function speed(inst)
    local MIN_SPEED = GetModConfigData("MIN_SPEED")
    local STEP_DOWN = GetModConfigData("STEP_DOWN")
    local MAX_SPEED = GetModConfigData("MAX_SPEED")
    local STEP_UP = GetModConfigData("STEP_UP")
   
        local racingbonus = 1
        local racingtask = nil
        if not racingtask then
            local function speedUp(inst)
                if not inst.is_running then
                    if racingbonus > MIN_SPEED then
                        racingbonus = racingbonus - STEP_DOWN
                        inst:SetExternalSpeedMultiplier(inst, "RacingBonus", racingbonus)
                    else
                        racingbonus = MIN_SPEED
                        inst:SetExternalSpeedMultiplier(inst, "RacingBonus", racingbonus)
                        if racingtask then
                            racingtask:Cancel()
                            racingtask = nil
                        end
                    end
                else
                    if racingbonus < MAX_SPEED then
                        racingbonus = racingbonus + STEP_UP
                        inst:SetExternalSpeedMultiplier(inst, "RacingBonus", racingbonus)
                    end
                end
            end
            racingtask = inst:DoPeriodicTask(0.2, speedUp)
        end
   
               
end

 

Edited by xDloko

One way you could do this is to have your racingbonus variable global and defined outside of the speed function. This way you can edit it in two different places and have a constant drain, while adding an amount while you move.

in your master_postinit you would have something along these lines

hasAddedSpeed = false
-- this performs the function when your character moves
inst:ListenForEvent("locomote", function (inst)
    -- this is to limit changes so it's at a constant rate
    if not hasAddedSpeed then
      -- uses both to counteract the drain
	  racingbonus = racingbonus + STEP_UP + STEP_DOWN
   	  -- limits the bonus to MAX_SPEED
      -- Your code wasn't properly doing this before and could go above your MAX_SPEED and below your MIN_SPEED
      if racingbonus > MAX_SPEED then
        racingbonus = MAX_SPEED
      end

      inst:SetExternalSpeedMultiplier(inst, "RacingBonus", racingbonus)
      
      -- ensure it won't edit until the time is over
      hasAddedSpeed = true
      --this should be the same time as the drain is
      inst:DoTaskInTime(0.1, function(inst)
          hasAddedSpeed = false
      end)
    end
end)

-- This sets a constant speed drain
-- 0.1 is an arbitrary number and you can change it
inst:DoPeriodicTask(0.1, function (inst)
    racingbonus = racingbonus - STEP_DOWN
    -- limits the bonus to above MIN_SPEED
    if racingbonus < MIN_SPEED then
      racingbonus = MIN_SPEED
    end
    
    inst:SetExternalSpeedMultiplier(inst, "RacingBonus", racingbonus)
end)

There are probably better ways to limit the additions per time than I used, but this should be able to work. Only issue might be a desync between adding and draining times so it might bounce back and forth, but I'm unsure of how to fix that other than having a really low time per change and having low numbers that it changes by at a time.

 

Edited by C3434
Adding a time control system to make it work as intended
  • Health 1

Sorry for not quickly responding

But currently i just make this one and works perfectly It does not have any problem in speed increase or decrease

just ignore bearset i only use it because my characters beard affects his speed

thanks for you help

On 7/27/2023 at 8:11 PM, C3434 said:

One way you could do this is to have your racingbonus variable global and defined outside of the speed function. This way you can edit it in two different places and have a constant drain, while adding an amount while you move.

in your master_postinit you would have something along these lines

hasAddedSpeed = false
-- this performs the function when your character moves
inst:ListenForEvent("locomote", function (inst)
    -- this is to limit changes so it's at a constant rate
    if not hasAddedSpeed then
      -- uses both to counteract the drain
	  racingbonus = racingbonus + STEP_UP + STEP_DOWN
   	  -- limits the bonus to MAX_SPEED
      -- Your code wasn't properly doing this before and could go above your MAX_SPEED and below your MIN_SPEED
      if racingbonus > MAX_SPEED then
        racingbonus = MAX_SPEED
      end

      inst:SetExternalSpeedMultiplier(inst, "RacingBonus", racingbonus)
      
      -- ensure it won't edit until the time is over
      hasAddedSpeed = true
      --this should be the same time as the drain is
      inst:DoTaskInTime(0.1, function(inst)
          hasAddedSpeed = false
      end)
    end
end)

-- This sets a constant speed drain
-- 0.1 is an arbitrary number and you can change it
inst:DoPeriodicTask(0.1, function (inst)
    racingbonus = racingbonus - STEP_DOWN
    -- limits the bonus to above MIN_SPEED
    if racingbonus < MIN_SPEED then
      racingbonus = MIN_SPEED
    end
    
    inst:SetExternalSpeedMultiplier(inst, "RacingBonus", racingbonus)
end)

There are probably better ways to limit the additions per time than I used, but this should be able to work. Only issue might be a desync between adding and draining times so it might bounce back and forth, but I'm unsure of how to fix that other than having a really low time per change and having low numbers that it changes by at a time.

 

Spoiler
--Speed system
local function speedS(inst)
    local function speedUP()
        if inst.components.locomotor:GetTimeMoving() >= 2 and inst.components.locomotor:GetTimeMoving() <= 4 then
            if inst.beardset == 1 then
                inst.components.locomotor.runspeed = 1.2 * TUNING.WILSON_RUN_SPEED + (inst.level*0.001) + 1.8
            elseif inst.beardset == 2 then
                inst.components.locomotor.runspeed = 1.4 * TUNING.WILSON_RUN_SPEED + (inst.level*0.001) + 1.8
            elseif inst.beardset == 3 then
                inst.components.locomotor.runspeed = 1.6 * TUNING.WILSON_RUN_SPEED + (inst.level*0.001) + 1.8
            end
        elseif 4 < inst.components.locomotor:GetTimeMoving() then
            if inst.beardset == 1 then
                inst.components.locomotor.runspeed = 1.2 * TUNING.WILSON_RUN_SPEED + (inst.level*0.001) + 3.6
            elseif inst.beardset == 2 then
                inst.components.locomotor.runspeed = 1.4 * TUNING.WILSON_RUN_SPEED + (inst.level*0.001) + 3.6
            elseif inst.beardset == 3 then
                inst.components.locomotor.runspeed = 1.6 * TUNING.WILSON_RUN_SPEED + (inst.level*0.001) + 3.6
            end
        else
            if inst.beardset == 1 then
                inst.components.locomotor.runspeed = 1.2 * TUNING.WILSON_RUN_SPEED + (inst.level*0.001)
            elseif inst.beardset == 2 then
                inst.components.locomotor.runspeed = 1.4 * TUNING.WILSON_RUN_SPEED + (inst.level*0.001)
            elseif inst.beardset == 3 then
                inst.components.locomotor.runspeed = 1.6 * TUNING.WILSON_RUN_SPEED + (inst.level*0.001)
            end
        end
    end
    local task = inst:DoPeriodicTask(0.2, speedUP)
end

 

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...