Question from a LOVE newbie

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.
Anooxy
Prole
Posts: 4
Joined: Sat Jul 23, 2011 1:49 am

Re: Question from a LOVE newbie

Post by Anooxy »

kraftman wrote:You could give each thing that you want to fade in its own timer.
What, how?
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Question from a LOVE newbie

Post by GijsB »

Do what i typed and add Some more D's if you know what i mean.
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Question from a LOVE newbie

Post by kraftman »

Anooxy wrote:
kraftman wrote:You could give each thing that you want to fade in its own timer.
What, how?
local myobject = {}
myobject.alpha = 0

local myobject2 = {}
myobject2.alpha = 0
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Question from a LOVE newbie

Post by GijsB »

diffrent timers example =

Code: Select all

timer1 = 0
timer2 = 0
timer3 = 0
function love.update(dt)
timer1 = timer1+dt
timer2 = timer2+dt
timer3 = timer3_dt
if timer1 > 1 then
timer1 = 0
--do stuff
end
if timer2 > 3 then
timer2 = 0
--do stuff
end
if timer3 > 10 then
timer3 = 0
--do stuff
end
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Question from a LOVE newbie

Post by bartbes »

Or, you know, use a single timer, but a better test. So something like:

Code: Select all

if timer > 10 and timer < 15 then
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Question from a LOVE newbie

Post by kraftman »

GijsB wrote:diffrent timers example =

Code: Select all

timer1 = 0
timer2 = 0
timer3 = 0
function love.update(dt)
timer1 = timer1+dt
timer2 = timer2+dt
timer3 = timer3_dt
if timer1 > 1 then
timer1 = 0
--do stuff
end
if timer2 > 3 then
timer2 = 0
--do stuff
end
if timer3 > 10 then
timer3 = 0
--do stuff
end
end

Using global variables as timers for objects is a pretty messy way of doing it. A better way would be to give each object its own timer, and use a loop to update the timers. Or to have a table that is looped through on update, and have objects that need adjust each update hook into that table.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Question from a LOVE newbie

Post by T-Bone »

If you were to use a lot of timers, you could create a class which takes care of a whole bunch of timers, store them in a table, sorts them based on when they are going to be activated, and then it just has to check the one closest to be activated all the time. I was thinking about doing something like this in my game.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Question from a LOVE newbie

Post by TechnoCat »

There are also libraries to aid in this http://vrld.github.com/hump/#timer
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Question from a LOVE newbie

Post by GijsB »

oke, i made this (i dont even know if it works :roll:)

Code: Select all

timers = {}

function love.update(dt)
for i,v in pairs(timers) do
v.time = v.time+dt
v.on = false
if v.time > v.maxtime then
v.time = 0
v.on = true
end
end
end

function addtimer(t)
newtimer = {time = 0, maxtime = t,on = false}
table.insert(timers,newtimer)
return timers.newtimer
end

timer1 = addtimer(10)

if timer1.on then--this doesnt work, but how CAN i make it work 3:?
--blabla
end

User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Question from a LOVE newbie

Post by kraftman »

GijsB wrote:oke, i made this (i dont even know if it works :roll:)

Code: Select all

timers = {}

function love.update(dt)
for i,v in pairs(timers) do
v.time = v.time+dt
v.on = false
if v.time > v.maxtime then
v.time = 0
v.on = true
end
end
end

function addtimer(t)
newtimer = {time = 0, maxtime = t,on = false}
table.insert(timers,newtimer)
return timers.newtimer
end

timer1 = addtimer(10)

if timer1.on then--this doesnt work, but how CAN i make it work 3:?
--blabla
end

This probably belongs in another topic. The code won't work for a number of reasons. It's worth reviewing how variable scoping works in Lua, as well as table indexing and the love.update() function.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 13 guests