Spawning enemies
In a vertical shoot-em-up such as this one, enemies and other level objects must be delivered as the camera scrolls by.
There are many ways to achieve this. My goal was being able to place any scene or node anywhere in the level, run the game, and have it just work.
Naturally, if you simply place objects in your level scene using the Godot editor and run your project, all the nodes will all be there at runtime and unfortunately, the engine doesn't offer much control over this process.
There is at the time I’m writing this a proposal which I think perhaps could help, called a Stash. The proposal link: https://github.com/godotengine/godot-proposals/issues/10212
In the meantime, I found a way to do something like this in GD Script and borrowed the "Stash" name for my own class.
During the scene initialization, the Stash interferes with the scene setup to remove its direct children from the tree before those activate _ready()
. The node instances are stored in an array and users of the Stash can decide later when to put those nodes back and how.
Long story short, here is some simplified Stash code:
# Stashed nodes. var instances:Array[Node] func _notification(what:int)->void: match what: NOTIFICATION_ENTER_TREE: _stash() NOTIFICATION_EXIT_TREE: _stash_free() # Store direct children in the array and remove them from the scene tree. func _stash()->void: instances=get_children() for n:Node in get_children(): remove_child(n) # Free non-null instances. func _stash_free()->void: instances.map(func(n:Node)->void:if n:n.free()) func spawn_everything()->void: while not instances.is_empty(): var n:Node=instances.pop_front() add_child(n)
NOTIFICATION_ENTER_TREE
happens before child nodes are ready.
In Godot parlance, this is essentially akin to intentionally "orphaning" those nodes, and they will indeed appear as orphans in the debug graph. The Stash takes care to delete leftover nodes in its array when it is freed, preventing actual memory leaks.
The final step in spawning enemies involves a small, camera-activated node I call a Trigger. All a Trigger does is wait for the camera to reach its y-position and then emit a signal. The Level creates a Trigger for every stashed instance at startup.
The video below demonstrates the process in action, showing the orphan count decrease as stashed instances are delivered. The red ellipses represent Triggers.
Get Protostar
Protostar
Retro shoot-em-up
Status | In development |
Author | zub_zob |
Genre | Shooter |
Tags | 8-Bit, Casual, Godot, Pixel Art, Retro, Shoot 'Em Up |
Languages | English |
Leave a comment
Log in with itch.io to leave a comment.