Trigger timer example
Running from local file ⚠️
It seems like you have opened this file by double-clicking on it. In order to test your build in a browser you need to load this file from a web server . You can either upload this file and the rest of the files from a Defold HTML5 bundle to a web hosting service OR host them using a local web server on your home network.
Learn more about running a local web server in the Defold HTML5 manual .
Setup
The example shows how to use Defold built-in timer and trigger it asynchronously and uses two indicators:
A counter text increased every 1s created using a text node.
A text node with information displayed.
The timer triggers update of the counter every 1s.
Click anywhere to trigger the callback of the timer asynchronously.
Scripts
trigger_timer.gui_script
function init ( self )
self . count = 0 -- <1>
local interval = 1 -- <2>
local repeating = true -- <3>
self . timer = timer . delay ( interval , repeating , function () -- <4>
self . count = self . count + 1 -- <5>
local node = gui . get_node ( "counter" ) -- <6>
gui . set_text ( node , self . count ) -- <7>
end )
msg . post ( "." , "acquire_input_focus" ) -- <8>
end
function on_input ( self , action_id , action )
if action_id == hash ( "touch" ) and action . pressed then -- <9>
timer . trigger ( self . timer ) -- <10>
end
end
--[[
1. Start the count with value 0.
2. We will use interval of 1 [s].
3. We will be repeating the timer endlessly.
4. Start the timer with interval (1s) and repeating (true) and pass a callback function.
Store the handle to the timer in self.timer.
5. The function will be called every 1s, so increase the count by 1 each time.
6. Get the counter text node.
7. Update the counter text node with an increased count.
8. Tell the engine that this game object wants to receive input.
9. If the user clicks.
10. Trigger the timer's callback function asynchronously using the saved self.timer handle.
--]]
If you want to play with these examples, you can get the project on Github.
Do you want to see more examples? Why not write a few yourself and submit a pull request? We love contributions.
GITHUB