Timers

From MaratisWiki
Jump to: navigation, search

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