Hey all, as promised we are continuing on our use of Enums in Haxe. To read the previous post, you can find it here. Now, let’s get into it.
What Are Advanced Enums
As mentioned before Enums can help you better represent important types in your code. For example, you can represent directions using an Enum and maintain flexibility and clarity rather than using numbers or strings. It also lets you better make use of the switch case statement and if statements, reducing the number of errors you could potentially have. So, what are advanced enums?
Advanced Enums are Enums that can take parameters. Essentially, they carry data. This is extremely powerful; it allows us to have more dynamic ways to represent data and types in our code. For example, you could represent your resistances in a game with this. This is known as an ADT in other languages; the full name is Abstract Data Types. So, that’s what they’re used for. How can you use them and what do they look like?
Advance Enum Use
You can create an ADT Like so:
As you can see, it’s just an Enum that takes a parameter. Note, just like a function you can even make these parameters optional. Simple right? Now, Haxe knows what your Enums of this type would look like. So, let’s use it in our code to create a monster with elemental resistance.
And there we go, making use of our enum. We’ve now created a monster with elemental resistance. Bam easy right? What else can we do with this? We can check the resistance, update it in our code. We can also easily check other types of resistances easily using this setup. For example, we could have water resistance affect fire resistance, and so on.
This is just the tip of the iceberg of what you can do with Enums. I hope this helps you and good luck Game Making!!