Jump to content

Regeneration


Recommended Posts

This is the easy way. Put this in your character LUA's master_postinit() or fn() function.

-- DoPeriodicTask calls the given function every X seconds.
-- I set it to 1.0. You can set it to e.g. 0.5 seconds if you want.
inst:DoPeriodicTask(1.0, function(inst)
	-- Do nothing if the player is dead or doesn't have a health component for whatever reason.
	if not inst.components.health or inst.components.health:IsDead() or inst:HasTag("playerghost") then
		return
	end
	
	-- Do whatever checks you want, e.g., like the if-statement above.
	
	-- You can use whichever numbers you want here e.g. DoDelta(-2.5) to deal 2.5 damage.
	-- Or DoDelta(5) to heal 5 hp. The 'true' tells it to NOT pulse and make a sound every
	-- time the healing happens. It gets annoying.
	inst.components.health:DoDelta(1, true)
end)

But then the task always runs, and your checks are being done all the time. If your checks are simple, then it's alright. If they require a lot of data from different sources and measuring things, then perhaps it would be better to turn the task on and off at will.
You can do that by adding this code to the character LUA, preferably at the top of the file because you will be using the functions at different spots in your code, so they need to be declared early. Here is the code:

local myregentask = nil

local function enableRegen(inst)
	-- Only do something if our task is not already running.
	if not myregentask then
		-- DoPeriodicTask calls the given function every X seconds.
		-- I set it to 1.0. You can set it to e.g. 0.5 seconds if you want.
		myregentask = inst:DoPeriodicTask(1.0, function(inst)
			-- Do nothing if the player is dead or doesn't have a health component for whatever reason.
			if not inst.components.health or inst.components.health:IsDead() or inst:HasTag("playerghost") then
				return
			end
			
			-- Do whatever checks you want, e.g., like the if-statement above.
			-- If you make sure to always disable the regen when the player dies and such,
			-- then you can skip checking a lot of things in here, making the task very light CPU-wise.
			
			-- You can use whichever numbers you want here e.g. DoDelta(-2.5) to deal 2.5 damage.
			-- Or DoDelta(5) to heal 5 hp. The 'true' tells it to NOT pulse and make a sound every
			-- time the healing happens. It gets annoying.
			inst.components.health:DoDelta(1, true)
		end)
	end
end

local function disableRegen()
	-- Only do something if our task is not already running.
	if myregentask then
		myregentask:Cancel()
		myregentask = nil
	end
end

Then you can call enableRegen(inst) or disableRegen() wherever you want, and since they take no parameters other than inst, you can hook them up to any event.

UPDATE: Note that I have corrected a few things over the last couple of minutes.

Edited by Ultroman
Link to comment
Share on other sites

isnt it easier to use the health component with "StartRegen" and "StopRegen" ?
So simply call inst.components.health.StartRegen(amount,period) when you want to start and inst.components.health.StopRegen() when you want to stop.
Where to put this depends on your conditions, most often you will use ListenForEvent to listen for your condition.

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