Jump to content

I need help with health regen (temperature or hunger reliant)


Recommended Posts

So im just barely getting into modding and im very very new so it might show. 

I'm trying to make a character who either regens based on temperature (higher = faster regen) or when he's at least half full hunger wise regens about 1hp/s (this was a fallback option when i realized this was a lot different than what ive done so far and i might not be able to figure it out)

I have no idea which functions I should be trying to use, start regen/stop regen based on if hunger >= 50% or the do delta one. The first one sounds easier to code but postings seem to say it causes issues. And most helpful postings I've seen about health.dodelta seem to be globally focused not singular character. If someone could point me to how to use either that would be helpful since the most complicated thing I've done yet is made a character give off heat and see in the dark and i kinda just mimick'd a campfire's settings for the heat radius. 

any help pointing me in the direction to start learning how to do this would be appreciated 

 
Link to comment
Share on other sites

I'm not an expert on coding efficiently by a long shot, so there could be a much better way of doing this.

But from my understanding, you'd likely wanna make use of how the temperature component pushes the event "temperaturedelta" whenever its changed for your character, so you can place a listener "inst:ListenForEvent("temperaturedelta", myFunction)" in your character's master_postinit. You can look at that specific PushEvent in the component to see what data it pushes as well (In this case, last and new)

You can make use of the function built into the temperature component "GetCurrent" to get your current temp, but if you're doing the above then data.new will be your current temp. The temperaturedelta event is going to be pushed a lot, and data.new will have a long decimal attached so I would recommend setting a local variable to = math.floor(data.new) (this rounds the number down, removing the long decimal) and then to prevent the rest of the your function from being called repeatedly, do the same with data.last and have an if statement check if the two are the same number then return if true to stop the rest of the function from going off if there wasn't a change in whole number. From there its as simple as checking if the new temp is higher then where you want to start the regen

For the regen itself, you can just use DoDelta. I'm not certain if there's a better way to make regen work but that seems to be whats used the most. Use a DoPeriodicTask to do inst.components.health:DoDelta() repeatedly.

Edit : If you decide you want to go with the hunger option instead, I can help with that too if needed

Good luck!

Example code in spoiler in case you want it :

Spoiler
-- In your character's master_postinit
inst:ListenForEvent("temperaturedelta", OnTemp)

-- Above the OnTemp function
local function regenfn(inst, temp)
	if inst.components.health and inst.components.health:IsDead() then -- Probably better to make it cancel the function on player death instead, but putting this here for now to stop any crashes.
		inst.temptask:Cancel()
		inst.temptask = nil
		return
	end
	if inst.components.health then -- This checks if the component exists on the instance, this will likely always exist but if it ever doesn't the game will crash here without this check.
		inst.components.health:DoDelta(1) -- 1 can be anything, including an equation for how much you want to regen
	end
end

-- Above, outside the postinits
local function OnTemp(inst, data)
	local newtemp = math.floor(data.new)
	local oldtemp = math.floor(data.last)
	if newtemp == oldtemp then
		return
	end
	
	if newtemp >= 30 and inst.temptask == nil then -- 30 can be whatever you want. Check for if the task were about to start is already started so we don't start it again
		inst.temptask = inst:DoPeriodicTask(1, regenfn, nil, newtemp) -- First parameter is how often in seconds the task will go off. The less often it goes off the better for efficiency sake, take this into account when deciding how much to regen each time it goes off. The timer also isn't always perfectly accurate. Second paramater is the function its calling, third is amount of time in delay, in this case nil so there is no delay. Finally the last parameter is an extra variable to give the function alongside inst. I'm not using this in the example regenfn but you can use it to make an equation for how much to regen
	elseif newtemp < 30 and inst.temptask ~= nil then -- If temp goes back below your threshold, turn the task off and set it back to nil for later checks
		inst.temptask:Cancel()
		inst.temptask = nil
	end
end

 

 

Edited by Merkyrrie
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...