Add and use named timers instead

Prevents multiple calls to function when it expires, some functions
should only be run once
This commit is contained in:
NullBite 2022-03-15 23:44:17 -04:00
parent 5694ab896f
commit 1bf4859501
Signed by: nullbite
GPG Key ID: 6C4D545385D4925A

View File

@ -42,7 +42,7 @@ function changeExpression(_damage, _expression, ticks)
end
HEAD.setUV(getExprUV(damage,expression))
wait(ticks, resetExpression())
namedWait(ticks, resetExpression, "resetExpression")
end
function setExpression(damage, expression)
face_damage=damage
@ -111,6 +111,25 @@ do
end
end
-- named timers (this one is mine but heavily based on the other) --
-- if timer is armed twice before expiring it will only be called once) --
do
local timers = {}
function namedWait(ticks, next, name)
-- main difference, this will overwrite an existing timer with
-- the same name
timers[name]={t=world.getTime()+ticks,n=next}
end
function tick()
for key, timer in pairs(timers) do
if world.getTime() >= timer.t then
timer.n()
timers[key]=nil
end
end
end
end
-- Tick function --
function tick()