Timers

From MaratisWiki
(Difference between revisions)
Jump to: navigation, search
m
 
(One intermediate revision 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 ===
 
=== Basic Timer ===
 
+
<nowiki>t = 0
    t = 0
+
function onSceneUpdate()
    function onSceneUpdate()
+
    t = t + 1 -- count + 1 every frames
        t = t + 1 -- count + 1 every frames
+
    print(t)
        print(t)
+
end</nowiki>
    end
+
  
 
=== Basic Timer + Action ===
 
=== Basic Timer + Action ===
 
+
<nowiki>t = 0
    t = 0
+
function onSceneUpdate()
    function onSceneUpdate()
+
    t = t + 1
        t = t + 1
+
    if t == 120 then
        if t == 120 then
+
        print("2 seconds")
            print("2 seconds")
+
        t = 0 -- reset timer to 0, so it will loop forever
            t = 0 -- reset timer to 0, so it will loop forever
+
        -- do something else here
            -- do something else here
+
        end
+
 
     end
 
     end
 +
end</nowiki>
  
 
=== Count Seconds ===
 
=== Count Seconds ===
 
+
<nowiki>ms = 0
    ms = 0
+
seconds = 0
    seconds = 0
+
function onSceneUpdate()
    function onSceneUpdate()
+
    ms = ms + 1
        ms = ms + 1
+
    if ms == 60 then  
        if ms == 60 then  
+
        seconds = seconds + 1
            seconds = seconds + 1
+
        ms = 0
            ms = 0
+
        print(seconds, "<- Seconds")
            print(seconds, "<- Seconds")
+
        end
+
 
     end
 
     end
 +
end</nowiki>
  
 
=== Start/Stop timer on key press ===
 
=== 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
  
     timestart = false
+
     if timestart == true then  
    t = 0
+
         t = t + 1  
    function onSceneUpdate()
+
        print(t, "<- time")
        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
 
     end
 +
end</nowiki>
  
 
[[Category:User manual]]
 
[[Category:User manual]]
 
[[Category:Lua scripting]]
 
[[Category:Lua scripting]]

Latest revision as of 16: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
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox