Page 2 of 2
Re: Can't we use multiple love.update() ?
Posted: Wed Mar 21, 2012 3:36 pm
by semihmasat
Thanks,
Actually i am c# developer, but i am going to buy a book about lua i believe.
Re: Can't we use multiple love.update() ?
Posted: Thu Mar 22, 2012 12:26 am
by Inny
Just because we can, and as there might be a need for it, we can setup an observer pattern for love.update.
Code: Select all
-- main.lua
local callbacks = {} -- hide this table in this lexical scope.
function register( cb )
table.insert( callbacks, cb )
end
function deregister( cb )
for i = 1, #callbacks do
if callbacks[i] == cb then
table.remove( callbacks[i] )
break
end
end
end
function love.update(dt)
local cblist = {} -- for safe usage with deregister function
for _, cb in ipairs(callbacks) do
table.insert(cblist, cb)
end
for _, cb in ipairs(cblist) do
cb(dt)
end
end
Code: Select all
-- movement.lua
movement = { clock = 0 }
function movement.update(dt)
movement.clock = movement.clock + dt
if movement.clock > 5 then
print( "5 Seconds have passed" )
deregister( movement.update )
end
end
register( movement.update )
Of course, usage of it is totally up to you.
Re: Can't we use multiple love.update() ?
Posted: Thu Mar 22, 2012 6:00 am
by tsturzl
That "require" call in your update loop is making me cringe man..
Use objectivity. Instead of files make objects. For example:
Code: Select all
--THIS IS BAD PRACTICE
--movement.lua
function love.load()
--code
end
function love.update()
--code
end
function love.draw()
--code
end
--THIS IS GOOD PRACTICE
--main.lua <- don't move things out to separate files unless you need to
movement={}
function movement:load()
--code
end
function movement:update(dt) --its will probably help somewhere along the lines to pass a timestep argument to your update function
--code
end
function movement:draw()
--code
end
Now with the separate methods for the movement object you can call in love.load/update/draw etc.
I don't however understand why you need another file/object to handle movement... You'd think this would be part of your player object, or another object like a puzzle piece(if you're making a puzzle game) not an independent object. I realize I might not know exactly what you're doing, but if I've got a pretty good idea, then I'm gonna say you're way off on how to go about doing this. I'm just warning you before you get to far in and start trying to do some really crazy work arounds to get everything to work, I've done this many times when I began programming.