Log In  
Log In  

It's been a while since I've made a game so I'm running into questions I probably had answers previously...my apologies if this is a rerun.

I typically use a table for each type of object - enemies, bullets, powerups, etc. - and then loop over each table checking to see if the collide or whatever. Something like...

for bk,b in pairs(bullets) do
    for ek,e in pairs(enemies) do    
        if collide(e,b) then ...do something... end
    end
end

And then there could be several of these depending on which objects interact with each other.

But I've also seen where you use one table for all objects and then check object types to see if they should interact.

for bk,b in pairs(bigtable) do
    for ek,e in pairs(bigtable) do    
        if e.type==1 and b.type==2 then 
            ...do something...
        end

        if e.type==3 and b.type==4 then 
            ...do something...
        end
    end
end

Is there any advantage to one of these methods over the other?

Personally, having a table per object type makes it easier to stay organized but having a ton of loops going feels troublesome and can be hard to manage. Likewise, looping twice over a giant table sounds less ideal as well.

So...just wondering. I'll probably keep coding how I always do because it works and keeps me sane, but was just thinking about the impact.

P#53152 2018-05-31 20:32 ( Edited 2018-06-01 06:26)

Using a bit of OOP I usually have only two sets:

  • particles (inc. bullets)
  • actors (inc. ammo, power up)

Rationale is that only particles (many) are interacting with actors (few).
Actors and bullets (particles) have a ‘side’ property (bad/good/neutral/any) removing the need for clear cut tables like ‘enemies’ or ‘good guys’ (ex: the infamous exploding barrel!).

Code skeleton:

function _update()
 for _,p in pairs(particles) do
  if (not p:update()) del(particles,p)
 end
end
— example bullet
function make_bullet(x,y)
 return add(particles, {
  x=x, y=y,
  update=function(self)
   for _,a in pairs(actors) do
    — collide code left to the reader ;)
    if self.side!=a.side and collide(self,a) then
     a:hit(self.dmg)
     return false
    end
  return true
 end
 })
end
P#53158 2018-06-01 02:26 ( Edited 2018-06-01 07:24)

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-05-25 19:12 | 0.011s | 2097k | Q:14