
I figured I’d share a quick tip that I found helpful in structuring my project in Godot 3.
Signals in Godot allow you to loosely couple any component in your game tree using the Observer Pattern. This means that any object A can communicate with any other object B simply by signing up to listen for a particular signal.
Neither object needs to explicitly reference or even be aware of the other object as long as they both agree on the same interface. This is very helpful when dynamically generating the game tree out of many scenes since the parent can connect together all of the children as needed.
This works great for simple setups, but as soon as you need to connect parts of the game tree that aren’t direct siblings or children, things can get hairy. You may find yourself creating "pass-through" signals to forward messages down or up the tree. Or you may start making several versions of the same signal to handle edge cases.
I followed a strategy of making "handshake" signals for any major scenes that needed to be accessed throughout the code. My base_level.gd forwards a signal level_handshake that would connect on to any interested receiver’s _on_level_handshake function. In this way, any dynamically generated scene that needs to accessed elsewhere can be broadcast to any node that is interested. It simply says, "hello anyone who cares: I’m the level and here is my reference". The receiver can simply save this and use it as it sees fit.
Following this simple strategy has drastically improved the flexibility of my code in regards to updates and refactors. It’s simple, but it’s effective!