How to handle aliens' movement and destruction in a Space Invaders clone (Godot 3.4.2)
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
Have you tried using
call_deferred?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)?