runtime event listener or gameloop listener

I think I may be having a problem with a runtime listener. In my TD game, I have an OnCollision function that is called by a runtime listener. This onCollision function deals with enemy walking into range-radius of defenders, enemies getting hit by projectiles and dying, and enemies encountering barricades, stopping…and starting to hack away at them.

All well and good.

But I also have a main gameloop that adds enemies per loop, checks for barricades that have been hacked through and removed in the last loop…and if so the enemy’s transition.to is reactivated so it can proceed on its way.

Now…during the onCollision function, there will be some enemies that die and some get removed, then, of course I remove them from the array (table.remove) and of course the enemy array ID numbers all shuffle down.

However…in the game loop I have a suspicion my ‘for = i to totalEnemies do…’ loops maybe being interrupted by rutime collision calls which may explain why some of my enemies are not doing what they’re told. Is that likely?

Is the answer then to set up an onCollision listener in the game loop? (and NOT make it runtime) If so…I’m a bit confused…does it HAVE to be linked to a display object or can I just do…

local LoopListener:addEventListener (“collsiion”, onCollision)

(and help will result in positive karma from me)

@Alex,

  1. Corona is single-threaded so you don’t need to worry about two separate loops overlapping.

  2. Regarding your loops.  In your question, you imply your using numerically indexed tables.  I suggest using object indexed tables instead.  Then you don’t have to worry about holes.

Tip: The latter style is also referred to as a “dictionary” because we’re using key-value pairs instead of numbers.

Ex:

Instead of this:

local gameObjects = {} ... -- Adding local tmp = someNewObject() gameObjects[#gameObjects+1]=tmp ... -- Removing in a collision call tmp.collision = function( self, event ) local index = table.indexOf(gameObjects, self ) -- find this object in table table.remove( gameObjects, index ) -- remove it from the table (shifts table automatically) display.remove( self ) -- destroy the object end tmp:addEventListener( "collision" )

Just do this:

local gameObjects = {} ... -- Adding local tmp = someNewObject() gameObjects[tmp]=tmp ... -- Removing in a collision call tmp.collision = function( self, event ) gameObjects[self] = nil -- remove it from the table display.remove( self ) -- destroy the object end tmp:addEventListener( "collision" )

Now, everywhere else you need to loop over your table do this:

for k,v in pairs( gameObjects ) do -- do something with v, the value/object end

This guarantees no gaps in your table.

  1. I personally attach collision listeners to the objects like I did above, I don’t use unified listeners except for rare cases.

@Alex,

  1. Corona is single-threaded so you don’t need to worry about two separate loops overlapping.

  2. Regarding your loops.  In your question, you imply your using numerically indexed tables.  I suggest using object indexed tables instead.  Then you don’t have to worry about holes.

Tip: The latter style is also referred to as a “dictionary” because we’re using key-value pairs instead of numbers.

Ex:

Instead of this:

local gameObjects = {} ... -- Adding local tmp = someNewObject() gameObjects[#gameObjects+1]=tmp ... -- Removing in a collision call tmp.collision = function( self, event ) local index = table.indexOf(gameObjects, self ) -- find this object in table table.remove( gameObjects, index ) -- remove it from the table (shifts table automatically) display.remove( self ) -- destroy the object end tmp:addEventListener( "collision" )

Just do this:

local gameObjects = {} ... -- Adding local tmp = someNewObject() gameObjects[tmp]=tmp ... -- Removing in a collision call tmp.collision = function( self, event ) gameObjects[self] = nil -- remove it from the table display.remove( self ) -- destroy the object end tmp:addEventListener( "collision" )

Now, everywhere else you need to loop over your table do this:

for k,v in pairs( gameObjects ) do -- do something with v, the value/object end

This guarantees no gaps in your table.

  1. I personally attach collision listeners to the objects like I did above, I don’t use unified listeners except for rare cases.