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:

/**
* ADTs take in a parameter as you can see here.
*/
enum ElementalAtk {
FireAtk(?dmg:Int);
WaterAtk(?dmg:Int);
LightningAtk(?dmg:Int);
MagnetoAtk(?dmg:Int);
IceAtk(?dmg:Int);
WindAtk(?dmg:Int);
PhysAtk(?dmg:Int);
}
/**
* Elemental Resistances.
* Elemental resistance of 100 means you will not be affected by the
* status effect, thus making it impossible to be caught on fire.
*
*/
enum ElementalResistances {
FireRes(?res:Float); // question mark means you don't have to enter the elemental resistance Float
WaterRes(?res:Float);
IceRes(?res:Float);
MagneticRes(?res:Float);
LightningRes(?res:Float);
WindRes(?res:Float);
PhysRes(?res:Float);
}

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.

/**
* ADTs take in a parameter as you can see here.
*/
enum ElementalAtk {
FireAtk(?dmg:Int);
WaterAtk(?dmg:Int);
LightningAtk(?dmg:Int);
MagnetoAtk(?dmg:Int);
IceAtk(?dmg:Int);
WindAtk(?dmg:Int);
PhysAtk(?dmg:Int);
}
/**
* Elemental Resistances.
* Elemental resistance of 100 means you will not be affected by the
* status effect, thus making it impossible to be caught on fire.
*
*/
enum ElementalResistances {
FireRes(?res:Float); // question mark means you don't have to enter the elemental resistance Float
WaterRes(?res:Float);
IceRes(?res:Float);
MagneticRes(?res:Float);
LightningRes(?res:Float);
WindRes(?res:Float);
PhysRes(?res:Float);
}
class Monsters {
//We default all the resistances here to 0.5 for all monsters
public var fireRes:Float = 0.5;
public var waterRes:Float = 0.5;
public var iceRes:Float = 0.5;
public var windRes:Float = 0.5;
public var magneticRes:Float = 0.5;
public var lightningRes:Float = 0.5;
public var physRes:Float = 0.5;
public var burnt:Bool;
public function new() {
//Set a specific resistance
this.setRes(FireRes(1.0)); //Sets the monsters Fire resistance to one.
this.setRes(IceRes(1.0)); //Sets the monster Ice resistance to one.
}
/**
* Allows you to set the resistance of an element by passing
* in the enum/ADT value.
* @param res ElementalResistance
*/
public function setRes(res:ElementalResistances) {
switch (res) {
case FireRes(res):
fireRes = res;
case WaterRes(res):
waterRes = res;
case IceRes(res):
iceRes = res;
case WindRes(res):
windRes = res;
case MagneticRes(res):
magneticRes = res;
case LightningRes(res):
lightningRes = res;
case PhysRes(res):
physRes = res;
}
}
}

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.

Conclusion

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!!

%d bloggers like this: