Thanks,
Actually i am c# developer, but i am going to buy a book about lua i believe.
Can't we use multiple love.update() ?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- semihmasat
- Prole
- Posts: 27
- Joined: Sun Mar 04, 2012 2:38 am
Re: Can't we use multiple love.update() ?
You must pay for your crimes against the earth.
Re: Can't we use multiple love.update() ?
Just because we can, and as there might be a need for it, we can setup an observer pattern for love.update.
Of course, usage of it is totally up to you.
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 )
Re: Can't we use multiple love.update() ?
That "require" call in your update loop is making me cringe man..
Use objectivity. Instead of files make objects. For example:
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.
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
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.
Who is online
Users browsing this forum: Google [Bot] and 9 guests