Well, if you are familiar with Godot Engine is expected that you already know this feature. A signal is an application of the Observer Pattern, this means that you will warn a giving object when something expected happens, then this object can properly act after that.
Now let’s talk about some tricks with signals which you can use in your favor to speed up your gamedev process!
1. Connecting to a member method
That’s very handy, you don’t actually need to create a new method every time you want something to happen when a signal is emitted, especially if this signal doesn’t carry data. For instance you can connect the signal
exit_screen of a VisibilityNotifier node to the
queue_free method of its parent. This is handy for projectiles for instance.
2. Using Extra Call Arguments
When a signal is emitted it can always carry data with it, even if by default it doesn’t. By adding extra call arguments you ensure your signal will pass this data with it, and this data can be used by the methods the signal is conneted to. For instance you can easily create a stylized TextureButton by adding a
Color as extra call argument for the
set_modulate method and connect the signals
button_down to it. You can use any kind of colors to simulate a hover, a pressed and a normal state. To make the Texture brighter make sure to turn on the RAW Mode of the ColorPicker and use a value greater than 1.0.
No code was hurt during the design of this custom button :P
3. GUI and SamplePlayer
It’s common that you have a SamplePlayer to play some sound effects in your game. So here goes a simple trick:
use a single SamplePlayer for your whole interface and use signals to play the sfx
By using the trick #2 you can connect signals like
focus_enter to the
play member method of the SamplePlayer and use a
string as extra call argument to tell which sound effect should be played when the player is interacting with your GUI.
4. Custom Signals
Ok you can’t do everything with signals, right? But imagine that you are in a team composed by three individuals: an engineer, an artist and a designer. You can ask your engineer, or if you are the engineer propose that to your team, to make custom signals for important events of your game, e.g.:
- A signal for a switch mechanic, so you can connect it to a method that changes the current state of an obstacle, such as a door.
- A signal for when a character HP is lower than 10% so you can connect it a ParticleEmitter so it can emit some kind of blood drops or a dying aura.
- A signal for when a character speed, especially vertically, is greater than a giving speed, so you can connect it to an AnimationPlayer and play a custom falling animation
This will make your life, your engineer and everybody else’s lives better, your workflow will be much smoother since the engineer will have to care about just the logic and let you set what happens when a condition triggers an event.
5. Prototyping with Signals and Tool mode
This is one of my favorites. Let’s say you want to test some of the above examples but you don’t want to execute the game, try to reach a giving condition and only then see if your custom signals and logics are working. Well, fear nothing, signals are perfect to use with a
A script with the keyword “tool” at its top is a script which will execute in the editor, you can think of them as debug tools, plugins etc…
That means you can connect a “tool signal” to any method of your other objects. This way you can test many of your behaviors without testing them as a player in runtime.
In this example when the variable reaches a value greater than 500 it emits a signal and when it reaches a value lower than 300 it emits another signal. This can be used to set the type of a damage based on its range, for instance, if lower than 300 it will be always a melee attack. This is tricky because it modifies the scene itself, so if you are making some characters you can start with a
basic_character.tscn modify the range of this character and save it as a new scene, it will change the type of the damage just as if you changed it manually.
Using signals this way is considered somehow a deterministic programming. By deterministic it means you always want that specific behavior when that specific thing is triggered. This doesn’t involves actual logic, so be careful, it is a work around for many situations but don’t think you can make a game using only signals, or at least not only the builtin ones.
Well that’s it, that’s all for now guys. I hope you enjoyed this, I’m on a rush for the Game Off Jam so is very likely that I don’t make videos this month.
That’s it, keep developing and until the next time.