Sometimes you want something to start on a delay or run some script at a time in the future. To do this, we can use a timer. Timers are created via the global Animator's 'createTimer()' function.
Here's a simple example of calling a function a few moments after an event fires:
function Trigger:onEnter(obj) getAnimator():createTimer(self, self.onTimeOut, seconds(2)) end function Trigger:onTimeOut() print("Time's up!") end
In this example, when an object passes through the Trigger and causes the 'onEnter()' event to fire, a timer is created that will wait 2 seconds, then call the Trigger's own 'onTimeOut()' function that, in this case, prints a message. Notice that for the second argument of 'createTimer()' we pass in a function as an object, hence the '.' (i.e. self.onTimeOut) and not ':' (i.e. self:onTimeOut). The ':' would signify to call the 'onTimeOut()' function, which is not what we want here.
Here's another example of creating a Trigger that creates a timer when the player enters it that requires you to reach a different Trigger in a set amount of time or else the object is deactivated. Here's the code for Trigger 1:
-- To store our object during the timer wait time Trigger.objToRemove = nil -- Function to stop the timer. Called by the other Trigger if -- our object makes it there in time function Trigger:stopTimer() -- Verify we are running a timer if (not self.objToRemove) then return end -- Clear out our variable stored in the object self.objToRemove.startTrigger = nil -- Stop the timer so it doesnt call our onTimeOut() function getAnimator():stopTimer(self, self.onTimeOut) -- Reactivate us so we can pass another object through self.active = true -- Clear state self.objToRemove = nil end -- Called when an object enters the trigger function Trigger:onEnter(obj) -- Deactivate us so this Trigger can't be re-triggered until the timer is stopped self.active = false -- Store our object so we can refer to it in our onTimeOut function self.objToRemove = obj -- Store this trigger inside the object so the other trigger can find us and stop -- the timer when the object enters it obj.startTrigger = self -- Create our timer. After 2 seconds, it will call our onTimeOut() function getAnimator():createTimer(self, self.onTimeOut, seconds(2)) end -- Function called when the timer's timeout is reached function Trigger:onTimeOut() -- Remove the object from the space self.objToRemove:fadeOutAndOrphan() -- Reactivate us so we can handle another object self.active = true end
Here's the code for Trigger 2:
function Trigger:onEnter(obj) -- Check to see if the object passing in has our start timer object -- which means the timer is currently active if (exists(obj.startTrigger)) then obj.startTrigger:stopTimer() end end
The main part of the example is the Animator's 'createTimer()' and 'stopTimer()' functions. With these functions, we can have the Animator call a function in the future. In the above example our future called function removes the object from the space if it isn't stopped in time.
Made It In Time
Did Not Make It In Time