How to make healing entities heal over time instead of instantly?
3 replies, posted
So I've been working on some consumable items for a roleplay server, but it's serious roleplay so it needs to stick as close to the game as possible. Is there a way to make it so eating something heals over time? For instance, eating a box of potatoes would restore 5 points all together, but only one point within, say, 10 seconds. Thanks in advance.
Very broad question.
Using a timer + SetHP +1 shouldn't be too hard?
Using something similar to this on the server should work.
[lua]function Player:Heal()
self.NextHeal = CurTime()
self.NumHeals = 5
end
local HealDelay = 3 -- seconds
local HealAmount = 1
hook.Add( "Think", "PlayerHealOverTime", function()
for _, ply in pairs( player.GetAll() ) do
if !IsValid(ply) or !ply:Alive() then continue end
if ply.NextHeal and ply.NextHeal < CurTime() and ply.NumHeals > 0 then
ply:SetHealth( ply:Health() + HealAmount )
ply.NextHeal = CurTime() + HealDelay
ply.NumHeals = ply.NumHeals - 1
end
end
end )[/lua]
[QUOTE=samm5506;38978119]Using something similar to this on the server should work.
[lua]function Player:Heal()
self.NextHeal = CurTime()
self.NumHeals = 5
end
local HealDelay = 3 -- seconds
local HealAmount = 1
hook.Add( "Think", "PlayerHealOverTime", function()
for _, ply in pairs( player.GetAll() ) do
if !IsValid(ply) or !ply:Alive() then continue end
if ply.NextHeal and ply.NextHeal < CurTime() and ply.NumHeals > 0 then
ply:SetHealth( ply:Health() + HealAmount )
ply.NextHeal = CurTime() + HealDelay
ply.NumHeals = ply.NumHeals - 1
end
end
end )[/lua][/QUOTE]
This would be quite an inefficient way to do it considering you are looping through every player every tick even when nobody is healing. Timers would be a better way to do it.
Something like this (Probably wont run straight out of the post and I just did it so you get the idea, would need some extra bits and pieces filled in)
Its a bit more advanced than what you asked, taking into account people eating more than one piece of food at a time. Heal amount and ticks are stacking and evenly awarded, for example a player with 2 heal ticks of 5hp each eats food worth 5 heal ticks of 25, he will heal 30hp over 7 ticks (around 4hp/tick) A bit more jiggery could be used to have dynamic delays on the timers but yeah, you get the point.
[lua]
function timedHeal(ply, amount, ticks)
if timer.IsTimer(ply:Nick() .. "heal") then
timer.Destroy(ply:Nick() .. "heal")
ply.healTick = ply.healTick + ticks
ply.healAmount = ply.healAmount + amount
local healPerTick = ply.healAmount / ply.healTick
timer.Create(ply:Nick() .. "heal", 3, ply.healTick, function()
ply:SetHealth(math.max(ply:Health() + healPerTick, 100))
ply.healTick = ply.healTick - 1
ply.healAmount = ply.healAmount - healPerTick
end)
else
ply.healTick = ticks
ply.healAmount = amount
local healPerTick = amount / ticks
timer.Create(ply:Nick() .. "heal", 3, ply.healTick, function()
ply:SetHealth(math.max(ply:Health() + healPerTick, 100))
ply.healTick = ply.healTick - 1
ply.healAmount = ply.healAmount - healPerTick
end)
end
end[/lua]
I'm not 100% on the new timers and passing arguments to them since I haven't done much coding for GM13, but I think this is correct.
Sorry, you need to Log In to post a reply to this thread.