Hey all, today we’re getting into signals in HaxeFlixel and how they allow you to decouple your code, making your life several times easier. You may not know what signals are, but after this you should have a good understanding of how beneficial they can be. Now, let’s get into signals.
What Are Signals
First, if you’ve ever used Godot or have messed with event listeners in Pixi.js, this is the same thing. Now, if you haven’t, signals in HaxeFlixel let you register a function to be called later when that signal is dispatched. That’s it; you can go home now! Jokes aside, this is very powerful, because it allows you to separate certain structures like your HUD from your player. Let me explain.
If you were creating a HUD for your game with a health bar and other player stats displayed, one solution would just be to pass in the player into the HUD and reference it. Additionally, you have to update the HUD in almost every frame to track changes on the player. That’s a completely viable solution, but now your player code and HUD code are now linked together. This isn’t the only issue you may have. If any of the other systems in your game need to modify the player or do something to the player, now you have to reference the player everywhere! What happens if you ever delete the player in a large game? You might end up in a null pointer and undefined maze as you forget where you reference the player anymore. That’s not so great; we have better solutions to these problems. We have the technology…we have signals.
Using signals, we can do better. Instead of updating the Player HUD every frame, we can update it only when we get a signal from the player. This could be a variety of different things. This could be for example when the player takes damage, shoots a gun, uses magic, and so on. All of these are valid uses for a signal. Using signals makes our game systems more akin to a response to action/state change, rather than a string of updates with conditional logic. With that said, how do you use a signal?
How To Use Signals
Using signals is all about making use of the FlxSignal class in Flixel. Here’s how you do it.
As you can see all we do is create the signal, with the parameters we want it to have; FlxTypedSignal allows us to create a signal with any parameters you want it to have. The standard signal, doesn’t take any parameters. Both of these are easy to use and only require one line of code. What’s the use of this? It allows you to only execute the code when the signal is dispatched; It’s asynchronous.
We can create a signal with the default signal class or a FlxTypedSignal, which allows us to make our own special signals. Once we create it, we can reference the signal anywhere else in the code and register callback functions to it. Now, other objects and systems in our game can respond to the signal and perform some function without directly referencing the object. Bam! Decoupling achieved.
With that said, I hope this helps you. Take advantage of signals in your own code to make things easier to reason about. Good luck game-making!! This is something I use in my programs all the time to make my life a lot easier. When you can describe part of your system as a stream of events reacting to one another, it can make things a lot easier.