Can't we use multiple love.update() ?

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.
User avatar
semihmasat
Prole
Posts: 27
Joined: Sun Mar 04, 2012 2:38 am

Re: Can't we use multiple love.update() ?

Post by semihmasat »

Thanks,

Actually i am c# developer, but i am going to buy a book about lua i believe.
You must pay for your crimes against the earth.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Can't we use multiple love.update() ?

Post 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.
User avatar
tsturzl
Party member
Posts: 161
Joined: Fri Apr 08, 2011 3:24 am

Re: Can't we use multiple love.update() ?

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

Who is online

Users browsing this forum: Google [Bot] and 9 guests