r/godot•
Versec

How to handle aliens' movement and destruction in a Space Invaders clone (Godot 3.4.2)

Help

TL,DR: How to handle movement and destruction of enemies stored in a List, moving one enemy each frame.

Hi people.

I'm making a Space Invaders clone, trying to be as accurate as possible to the original arcade game. In the original game, the game runs at 60fps, has 55 aliens (5 row and 11 columns) and every frame one alien moves 2 pixels left or right, starting from the bottom left alien, and ending on the top right one.

Currently, I'm placing my Alien nodes using a TileMap, then placing the Alien nodes and clearing the TileMap (this is due to the limitations of TileMaps in Godot 3.*, and it worked well for a Breakout clone I did before). On the Main node on the _ready() method I get all of the Alien children of the TileMap and put them in a reordered list so I can traverse through it every tick of the physics_process() method (since it runs at 60 fps), starting from the bottom left Alien, like in the original game, and move them:

   

func _ready():
alienList = $AlienMap.get_children()
for i in range(5,0, -1):    //reorder and get the Aliens in the order I want
	for j in range (11, 0, -1):
		var index = i*11 - j
		alienList[index].listIndex = index
		reorderedList.append(alienList[index])

   

func _physics_process(delta):
    // AlienCounter is an onready var that starts at 0
    if is_instance_valid(reorderedList[AlienCounter]):
        reorderedList[AlienCounter].position.x += 2    // move the alien. Right now it just moves to the right
	     if (AlienCounter>= reorderedList.size() -1): // If the game gets to he end of the list, start again
		    AlienCounter= -1
    AlienCounter +=1

   

My objective is to delete an Alien when it is hit, so the Aliens start to move faster as the orderedList becomes shorter, like in the original game. That's where the problem comes from: If I send a signal to the Alien that has been hit so it can queue_free(), then the game crashes when the physics loop comes back to the position on the list of that Alien (which is understandable). I have tried also to remove() from the List the alien that has been destroyed, but depending on the value of AlienCounter it crashes (when the List grows shorter but at the same time AlienCounter is higher).

I have also tried different methods of solving the problem or circunventing it, but nothing I come up with works well enough. I have now the feeling that my approach using a List is wrong, but I can't come up with anything else.

¿Any ideas on how to fix it? Thanks in advance.

Posted May 4, 2022, 1:59 PM UTC

29
Nkzar•

Have you tried using call_deferred?

1
VersecOP•

Hi, sorry for the late response. I did try a couple of things with call_deferred(), but didn't get anywhere, I have looked at the docs but have never understood complete what call_deferred() does, other than waiting for execution until "idle time" (whatever that means). How would you use it and what would you put in the deferred call (removing the alien from the List, doing queue_free() or something else)?

1
_Denny__•

I can recommend Dictionaries with an is_active field value. Count not the size count the active. What the engine do in background for removing…you don’t care.

1
VersecOP•

Can you make a quick example of how would you use a Dictionary for this?

1
_Denny__•

This is an example from a bullet_management, the idea is the same...replace bullets with enemy, use your needed properties and go.

​

When fired, it creates bullets and store them, as I need different groups for damage, friendly, enemy .... it is a bit more

The giveaway here...hashvalue as ID

func add_bullet(position_b: Vector2, movement: Vector2, damage: int, lifetime: float, owner: Node2D):

    var bullet = {
    	"position": position_b,
    	"movement": movement,
    	"damage": damage,
    	"lifetime": lifetime,
    	"owner": owner
    }
    
bull_conti[bullet.hash()] = bullet

in my _physics function i create and array

var drop_bullets = []

during the physics call, it performs raycasting and movement stuff...

whenever the bullet hit something or reaches end of life it gets added to this array.

the last step is to check if this array is empty

if not drop_bullets.empty():
    drop_bullet_dic(drop_bullets)

and remove the items

func drop_bullet_dic(drop: Array):
    for i in drop:
        bull_conti.erase(i)

Dictionary are pretty sweet performant if you need to remove stuff between and whatever I tried before with arrays, ended in a mess.

1
VersecOP•

Mmmm... I still can't see, and I don't think your solution applies to my problem. My problem is I need to iterate through the Aliens in order, even if Aliens are deleted/skipped because they have been destroyed; which I think is not an issue that applies to bullets (well, maybe, it's your game). The thing is I can't come up with a system that handles deleting/skipping aliens and goes to next that hasn't been destroyed on the same physics frame.

Do you iterate through the bullet dictionary using the hash, or something else? If so, how do you go to the next bullet?

1
_Denny__•

My objective is to delete an Alien when it is hit, so the Aliens start to move faster as the orderedList becomes shorter, like in the original game.

you can iterate like on a normal arrays. Honestly 60 Aliens should not an issue, regardless what you use.

The benefit here is just readability. Create a field "destroyed" for not counting or even "grid,row, index, position". What ever your loop help to sort the stuff.

I do loop over all bullets, but as some of them hit earlier and i need to delete them in the middle. Thats tricky with normal arrays and here is accessing with hash_id perfect.

1
billsongames•

I did a remake of Invade-a-Load, the Commodore 64 Space Invaders loading game. I seem to recall that I had each column of aliens set up as a Node2D with the aliens themselves as child nodes. The parent node kept track of how many alien children were left. Aliens were removed by queue_free.

You have to allow for the movement variations for both speed and how far left and right the alien columns move. Having the parent node keep track of its children helps with this I found.

I should be able to dig the code out if needed.

1

Nothing to do with what you asked, but if you want to make it pixel perfect then you could probably check that by using an emulator with some version of Space Invaders running and taking background inputs, record some fiddling around in OBS, and go frame by frame.

Maybe see if you can find a cheat in the emulated version to prevent the aliens from shooting so you can do diagnostics that don't have to do with getting shot (at) without hassle.

0