Page 1 of 1
Time Based Events.
Posted: Thu Feb 18, 2010 4:01 pm
by TylerKinkade
Hello Löve-ites.
I have a question about time.
Lets say every ten seconds I want something to spawn. How do I go about implementing that?
This is what I've tried to far:
Code: Select all
powerTicks = 0
function activePower(dt)
--timeA = a static point in time defined in love.load
timeB = love.timer.getMicroTime() -- ripping
if timeB - timeA == 10 and powerTicks == 0 then
timeB = timeA
powerTicks = 1
else
end
end
function drawPowerTime()
if powerTicks == 1 then
love.graphics.print("##########",200,250)
love.graphics.print("##########",200,260)
love.graphics.print("##########",200,270)
love.graphics.print("##########",200,280)
else
--just for testing...
love.graphics.print(timeB,200,300)
love.graphics.print(timeA,200,290)
end
end
Much Löve,
Kinkade
Re: Time Based Events.
Posted: Thu Feb 18, 2010 4:42 pm
by Virox
I would use this construct
Code: Select all
function activePower(dt)
if varTime >= 10 then
-- execute event
else
varTime = varTime + dt
end
end
dt or deltatime is the time in seconds between two displayed frames.
varTime would be a global variable declared in the load
Re: Time Based Events.
Posted: Thu Feb 18, 2010 4:45 pm
by TylerKinkade
Awesome awesome thank you much
Re: Time Based Events.
Posted: Thu Feb 18, 2010 4:54 pm
by pygy
I'm currently debugging Polygamy.timer which will allow the following things:
Code: Select all
my_timer = Polygamy.timer {
delay = time, -- the delay in seconds before the first occurrence. 0 if not specified
times = integer, -- How many times do you want to execute it? 1 if not specified, 0 = infinite
interval = time; -- the interval in seconds between each repeats. set to "delay" if not specified.
function(times, countdown)
-- do whatever you want to do repeatedly
-- <times> is the number of times the timer has been executed and
-- <countdown> is how many times are left before it expires (0 if infinite)
end
}
my_other_timer = Polygamy.timer{ etc ... }
my_timer:pause()
my_timer:resume(delay)
Code: Select all
Polygamy.timer:pause() -- pauses all timers
Polygamy.timer:resume() -- resumes all timers previously active. The ones paused individually are still paused.
Polygamy.timer refers to the default timeline, but you can create new ones, that can be enabled and paused individually.
Code: Select all
Polygamy.timeline("my timeline"){ a timer request object}
Stay posted :-)
Re: Time Based Events.
Posted: Thu Feb 18, 2010 5:00 pm
by Robin
Note that this approach (the one of the OP) doesn't scale well: if you have more than a few events, you'll have globals all over the place.
Some sort of "event queue" might solve this in a better way if you want to write a larger game. For example:
Code: Select all
eventQueue = {
[6]=function() powerTicks = true end,
[11]=function() --[[do something awesome]] end
}
totalTime = 0
function update(dt)
totalTime = totalTime + dt
for time, event in pairs(eventQueue) do
if time < totalTime then
event()
eventQueue[time] = nil
end
end
end
(Standard warning:) This is completely untested.
Re: Time Based Events.
Posted: Thu Feb 18, 2010 5:12 pm
by TylerKinkade
Wow, excellent support all around. Thanks guys.
The event system seems extremely practical for what my team is working on.
I'll definitely look into working it in.
<3