Timer

I am making my first app and I’m and trying to make it so every .5 seconds it adds one point the score. How could I do this?

.@Andrewsimpson4 Here’s a start.


function setup()
    t=ElapsedTime
    score=0
end

function draw()
    background(40, 40, 50)
    fill(255)
    if ElapsedTime-t>.5 then
        score = score + 1
        t=ElapsedTime
    end
    text(score,WIDTH/2,HEIGHT/2)
end

I implement that way to be faster (saVes on “+” at each draw…Mmmm… Maybe i am a maniac.)



function setup()
    t=ElapsedTime + 0.5
    score=0
end

function draw()
    background(40, 40, 50)
    fill(255)
    if ElapsedTime>t then
        score = score + 1
        t=ElapsedTime + 0.5
    end
    text(score,WIDTH/2,HEIGHT/2)
end

Here are 2 scheduling functions that cleverly (hackishly?) take advantage of tween:

-- schedule 

local _empty = {}

function once(delay, fn, ...)
    local _elapsed = ElapsedTime
    return tween(delay, _empty, _empty, 'linear', function(...)
        fn(ElapsedTime - _elapsed, ...)
    end, ...)
end

local function _updateargs(...)
    if select('#', ...) > 0 then return {...} end 
end

function schedule(delay, fn, ...)
    local t, _update, _args, _elapsed = {}
    
    local _loop = function()
        _elapsed = ElapsedTime
        t.id = tween(delay, _empty, _empty, 'linear', _update)
    end
    
    _update = function()
        local elapsed = ElapsedTime - _elapsed
        
        _loop()        
        
        if _args then
            fn(elapsed, unpack(_args)) 
        else 
            fn(elapsed)
        end
    end            
    
    t.stop = function()
        if not t.id then return end
        tween.stop(t.id)
        t.id = nil
    end
    
    t.start = function(_delay, _fn, ...)
        t.stop()
        delay, fn, _args = _delay or delay, _fn or fn, _updateargs(...)
        _loop()
        return t
    end
    
    return t.start(nil, nil, ...)
end

For a repeating timer, use:


timer = schedule(0.5, function()
    score = score + 1
end

-- to stop the timer:
timer.stop()

-- to restart the timer:
timer.start()

If you just need a one-off timer, use:


once(5, function(dt)
     print('Called once after', dt, 'seconds')
end)

EDIT: This code was experimental and is pretty crappy. The timer code I posted below is much better.

A much better attempt, that doesn’t use tween:

https://gist.github.com/apendley/5382684

EDIT: revised:

  1. Timer.lua no longer depends on Codea's class() function; it is now generic enough to be used independent of Codea.
  2. AutoTimer.elapsed changed to AutoTimer.update
  3. Use Timer/AutoTimer to have the callback pass no additional arguments to the timer callback.
  4. Use TimerDT/AutoTimerDT to have the timer pass an additional argument to your timer callback containing the elapsed time since the callback was last invoked (or since the timer was started)
  5. Use TimerE/AutoTimerE to have the timer pass an additional argument to your timer callback containing timer event data. Event data contains fields:
    • eventdata.timer: timer calling callback
    • eventdata.dt: elapsed time since callback last invoked (or since the timer was started)
    • eventdata.n: number of times callback has been invoked since the timer was started