Jump to content

Code for when character gets at certain, hunger, sanity, health?


Recommended Posts

I need some help trying to get some code to work, so you see, I'm trying to get my character to have code like when he gets at 200 hunger he gets new changes then we gets at 180 hunger he gets new changes then when he gets 150 hunger he gets new changes, this just example but I wanna try code like that, so I did this...

inst:DoPeriodicTask(1,
function()
if (self.hunger.current >= (self.hunger.max * 200)) then
inst.components.locomotor.runspeed = 10.15 * TUNING.WILSON_RUN_SPEED
end
end)

inst:DoPeriodicTask(1,
function()
if (self.hunger.current >= (self.hunger.max * 180)) then
inst.components.locomotor.runspeed = 1.15 * TUNING.WILSON_RUN_SPEED
end
end)

inst:DoPeriodicTask(1,
function()
if (self.hunger.current >= (self.hunger.max * 150)) then
inst.components.locomotor.runspeed = 10.15 * TUNING.WILSON_RUN_SPEED
end
end)

but it doesn't work :(, does anyone know a way to make stuff happen when health, sanity and hunger go at a certain number?! I would really love some help :)!

Link to comment
Share on other sites

In your character.lua:

local function hungerdelta(inst)
	if inst.components.hunger.current >= 200 then
		inst.components.locomotor.walkspeed = 10.15 * TUNING.WILSON_WALK_SPEED
		inst.components.locomotor.runspeed = 10.15 * TUNING.WILSON_RUN_SPEED
	elseif inst.components.hunger.current >= 180 then
		inst.components.locomotor.walkspeed = 1.15 * TUNING.WILSON_WALK_SPEED
		inst.components.locomotor.runspeed = 1.15 * TUNING.WILSON_RUN_SPEED
	elseif inst.components.hunger.current >= 150 then
		inst.components.locomotor.walkspeed = 10.15 * TUNING.WILSON_WALK_SPEED
		inst.components.locomotor.runspeed = 10.15 * TUNING.WILSON_RUN_SPEED
	else
		inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED
		inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED
	end
end

 

In your character.lua's master_postinit:

inst:ListenForEvent("hungerdelta", hungerdelta)

 

Edited by DrSmugleaf
Link to comment
Share on other sites

When checking stats, its best to just use a listen event

--character.lua

--anywhere outside, as long as it above the postinits
local function OnHungerChange(inst, data)
    local hungerpct = data.newpercent
	local speedmult = 1 -- 
    if hungerpct > 0.75 then -- hunger is above 75%
        speedmult = 7.5
    elseif hunger > 0.5 then -- hunger is above 50%
        speedmult = 5
    elseif hunger > 0.25 then -- hunger is above 25%
        speedmult = 2.5
    end--if its below 25% it will stay as 1

    if inst.components.locomotor.runspeed ~= speedmult * TUNING.WILSON_RUN_SPEED then
        inst.components.locomotor.runspeed = speedmult * TUNING.WILSON_RUN_SPEED
    end
end


--masterpostinit
inst:ListenForEvent("hungerdelta", OnHungerChange)

I think the problem with your version is that, 1 you're getting the values badly and 2 you're multiplying your max hunger by 150 for some reason

Link to comment
Share on other sites

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
 Share

×
  • Create New...