Time Based Events.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
TylerKinkade
Prole
Posts: 5
Joined: Wed Feb 17, 2010 7:11 am

Time Based Events.

Post 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
User avatar
Virox
Citizen
Posts: 64
Joined: Thu Jan 07, 2010 6:24 pm

Re: Time Based Events.

Post 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
User avatar
TylerKinkade
Prole
Posts: 5
Joined: Wed Feb 17, 2010 7:11 am

Re: Time Based Events.

Post by TylerKinkade »

Awesome awesome thank you much :)
User avatar
pygy
Citizen
Posts: 98
Joined: Mon Jan 25, 2010 4:06 pm

Re: Time Based Events.

Post 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 :-)
Last edited by pygy on Thu Feb 18, 2010 5:10 pm, edited 6 times in total.
Hermaphroditism is not a crime. -- LSB Superstar

All code published with this account is licensed under the Romantic WTF public license unless otherwise stated.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Time Based Events.

Post 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.
Help us help you: attach a .love.
User avatar
TylerKinkade
Prole
Posts: 5
Joined: Wed Feb 17, 2010 7:11 am

Re: Time Based Events.

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests