Advice on moving groups of instances

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Krumpet
Prole
Posts: 12
Joined: Thu Dec 03, 2020 4:59 am

Advice on moving groups of instances

Post by Krumpet »

Note: Jump to the final paragraph for the questions if you don't want to read the back story. Thanks.

When I started building my Space Invaders clone, I considered the movement of the grid of enemies and thought, "Well, it sure seems inefficient to loop through this table of enemy objects, update them all, and then render them all. What if I put them in a container and then moved the container instead? When the container moves, the enemies will move."

However, I quickly learned that moving the container did not result in the enemy objects moving. Which made sense after I thought about them being different objects.

Next, I set the enemy object up so each was instantiated with an x-position that was something like this...

Code: Select all

self.x = 20 + container.x
Again, updating the container position didn't move the enemies. It dawned on me that, in order to move anything in LOVE (and possibly any game engine), a rendering event must take place. You can't simply render once and then move something around by updating a property.

This brought me all the way back to the start. If I have to update the position of every enemy and render it, then I don't think placing them in a container is gaining me any efficiency.

My question is this...is there a more efficient way to render and move a collection of objects than looping through each instance? I'm not worried about making my Space Invaders clone work...I can do that. I'm considering the general approach and how it will (or will not) scale up to larger instance collections. In essence, I'm using this clone as a way to identify "better" practices before I try to tackle a larger project.

Edited for clarity
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Advice on moving groups of instances

Post by zorg »

There are a few ways of moving things simultaneously, and ways of doing it in a very optimized way as well.

To preface all of this though, for a "simple" space invaders clone, iterating through 50-100 entities won't impact performance at all, so it's not really necessary to do any of the more complicated solutions; with that said:

Löve redraws the whole screen each frame by default, unless you draw to a canvas object, which retains the data (it might get clobbered if you resize though)

Now, self.x = 20 + container.x should actually have worked, if you modified container.x and then drawn entities with their self.x-es.

Another solution would be to always render to the same place, but use love.graphics.translate to set the offset of everything (e.g. to container.x or whatever)

Finally, you could use a canvas to draw only once, then draw the canvas itself at the correct offset (again, e.g. container.x),

All of these have upsides and downsides of course, but for a simple game with such few entities, the performance impact of any of these is negligible.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Advice on moving groups of instances

Post by pgimeno »

To add another possibility, you can also use a scene tree, which intuitively works the way you expected. If you move the parent, all children automatically follow it. Unfortunately, Löve doesn't come with built-in scene graph management as e.g. Amulet or Godot do; you need a library like https://love2d.org/forums/viewtopic.php?f=5&t=85947 or https://love2d.org/forums/viewtopic.php?f=5&t=89348 or make one yourself.
User avatar
Krumpet
Prole
Posts: 12
Joined: Thu Dec 03, 2020 4:59 am

Re: Advice on moving groups of instances

Post by Krumpet »

These recommendations are exactly what I was hoping for. Greatly appreciated!

zorg - For something like Space Invaders where all enemies move an equivalent amount simultaneously (no independent movement), drawing to the canvas object and then moving said object seems ideal. Thanks for the input!

pgimeno - Great to see small libraries like those you linked to. I'll dig into that approach next.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Advice on moving groups of instances

Post by zorg »

Do note that in the event of needing to get rid of one of the invaders due to newly acquired existence failure by bullet, for instance, you can either clear the canvas completely, and redraw the new state with one fewer invader, or look into blend modes a bit, and i think you can just replace a rectangular area with transparency to basically delete only that off the canvas.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Krumpet
Prole
Posts: 12
Joined: Thu Dec 03, 2020 4:59 am

Re: Advice on moving groups of instances

Post by Krumpet »

Thanks again. My mind was drifting toward the question of, "Hmmm...how does this impact my ability to still interact directly with enemy instances?" Appreciate you mentioning it.
Post Reply

Who is online

Users browsing this forum: No registered users and 56 guests