Timers
From MaratisWiki
(Difference between revisions)
(Created page with "Some small snippets about timers, You can just copy/paste them in your script, results will be displayed in the maratis console window ----- ===== Basic Timer ===== ----- ...") |
|||
| (2 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
| − | Some small snippets about timers, | + | Some small snippets about timers, You can just copy/paste them in your script, results will be displayed in the maratis console window |
| − | You can just copy/paste them in your script, | + | |
| − | results will be displayed in the maratis console window | + | |
| − | + | ||
| − | + | === Basic Timer === | |
| − | + | <nowiki>t = 0 | |
| − | + | function onSceneUpdate() | |
| − | + | t = t + 1 -- count + 1 every frames | |
| − | + | print(t) | |
| − | + | end</nowiki> | |
| + | |||
| + | === Basic Timer + Action === | ||
| + | <nowiki>t = 0 | ||
| + | function onSceneUpdate() | ||
| + | t = t + 1 | ||
| + | if t == 120 then | ||
| + | print("2 seconds") | ||
| + | t = 0 -- reset timer to 0, so it will loop forever | ||
| + | -- do something else here | ||
end | end | ||
| − | + | end</nowiki> | |
| − | === | + | === Count Seconds === |
| − | + | <nowiki>ms = 0 | |
| − | + | seconds = 0 | |
| − | + | function onSceneUpdate() | |
| − | + | ms = ms + 1 | |
| − | + | if ms == 60 then | |
| − | + | seconds = seconds + 1 | |
| − | + | ms = 0 | |
| − | + | print(seconds, "<- Seconds") | |
| − | + | ||
end | end | ||
| − | + | end</nowiki> | |
| − | ===== | + | === Start/Stop timer on key press === |
| − | + | <nowiki>timestart = false | |
| − | + | t = 0 | |
| − | + | function onSceneUpdate() | |
| − | + | if onKeyDown("SPACE") and timestart == false then | |
| − | + | timestart = true | |
| − | + | print("START") | |
| − | + | elseif onKeyDown("SPACE") and timestart == true then | |
| − | + | timestart = false | |
| − | + | print("STOP") | |
| − | + | ||
end | end | ||
| − | |||
| − | + | if timestart == true then | |
| − | + | t = t + 1 | |
| − | + | print(t, "<- time") | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
end | end | ||
| − | + | end</nowiki> | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
[[Category:User manual]] | [[Category:User manual]] | ||
[[Category:Lua scripting]] | [[Category:Lua scripting]] | ||
Latest revision as of 15:50, 15 February 2014
Some small snippets about timers, You can just copy/paste them in your script, results will be displayed in the maratis console window
Contents |
Basic Timer
t = 0
function onSceneUpdate()
t = t + 1 -- count + 1 every frames
print(t)
end
Basic Timer + Action
t = 0
function onSceneUpdate()
t = t + 1
if t == 120 then
print("2 seconds")
t = 0 -- reset timer to 0, so it will loop forever
-- do something else here
end
end
Count Seconds
ms = 0
seconds = 0
function onSceneUpdate()
ms = ms + 1
if ms == 60 then
seconds = seconds + 1
ms = 0
print(seconds, "<- Seconds")
end
end
Start/Stop timer on key press
timestart = false
t = 0
function onSceneUpdate()
if onKeyDown("SPACE") and timestart == false then
timestart = true
print("START")
elseif onKeyDown("SPACE") and timestart == true then
timestart = false
print("STOP")
end
if timestart == true then
t = t + 1
print(t, "<- time")
end
end