Topic: Timer in lua

Hi,
I am currently making time challenges for my game, but I don't know how to make a timer to count the seconds. Does anyone know how?

Re: Timer in lua

Hello, to quote anael :

anael wrote:

The lua script update is syncronised at 60fps
so you can create a timer with a simple variable like that :

t = 0

function onSceneUpdate()

    -- 1 second
    if t == 60  then
        print("one second")
    end

    t = t+1
end

So you could do something like :
--print every seconds in the console

TIMER = {ms = 0,  seconds = 0, onesec = 60}

function onSceneUpdate()

    TIMER.ms = TIMER.ms + 1

    if TIMER.ms == TIMER.onesec then
        TIMER.ms = 0
        TIMER.seconds = TIMER.seconds + 1
        print(TIMER.seconds)
    end

end

Re: Timer in lua

Thanks! But if the fps was lower than 60 wouldn't the timer be inaccurate?

Re: Timer in lua

onSceneUpdate is always launched 60 time per second, even if the fps are lower. so it's safe.

Re: Timer in lua

Okay good, thanks.