Waiting, calling a function and waiting again.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Waiting, calling a function and waiting again.

Post by Anxiety »

How can i wait 10 seconds, call a function, wait 10 seconds again and call the same function? Like an endless loop to wait and call a function.

Still sorry for multiple posts :/
I can't come up with a good signature!
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Waiting, calling a function and waiting again.

Post by nevon »

Create a timer, decrease the timer by dt each frame, once the timer is 0 or less, run the function and reset the timer. Rinse and repeat.
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Waiting, calling a function and waiting again.

Post by Anxiety »

Now how do i create the timer?
I can't come up with a good signature!
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Waiting, calling a function and waiting again.

Post by vrld »

Anxiety wrote:Now how do i create the timer?

Code: Select all

timer, called = 0, 0
function love.update(dt)
    timer = timer + dt
    if timer > 10 and called < 2 then
        the_function(with, arguments)
        timer = 0
        called = called + 1
    end
end
Or with hump.timer:

Code: Select all

Timer.addPeriodic(10, function() the_function(with, arguments) end, 2)
function love.update(dt)
    Timer.update(dt)
end
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Waiting, calling a function and waiting again.

Post by nevon »

Anxiety wrote:Now how do i create the timer?
A timer is just a variable holding a number, so:

Code: Select all

function love.load()
    timer = 10
end

function love.update(dt)
    if timer <= 0 then
        letsRunOurFancyFunction()
        timer = 10
    else
        timer = timer - dt
    end
end
EDIT: Ninja'd by vrld!
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Waiting, calling a function and waiting again.

Post by Anxiety »

Ha. I thought i had to use something like timer.interval = 10000 and timer.tick but this easy, awesome!
I can't come up with a good signature!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 248 guests