Extending Enums

This guide will introduce you to the EnumHelper class, and will provide examples for extending various enums found in the game.

The EnumHelper module is fully generic, and works with any enum, even those which are not part of the vanilla game. Consider the following code:

 1enum Shapes
 2{
 3  Circle,
 4  Triangle
 5}
 6
 7class Shape
 8{
 9  public Shapes Type;
10
11  public Shape(Shapes type)
12  {
13    this.Type = type;
14  }
15}
16
17var circle = new Shape(Shapes.Circle);
18Logger.Log($"circle type is ${circle.Type}");
19//> circle type is Circle

The above code snippet creates a circle shape, however, it can only be a circle or a triangle, what if we wanted a square? For this, we can use the EnumHelper.ExtendEnum function, like so:

20int squareValue = EnumHelper.ExtendEnum<Shapes>("Square"); // this function returns the corresponding integer
21var square = new Shape(squareValue);
22Logger.Log($"square type is ${square.Type}");
23//> square type is 2
24// Circle = 0, Triangle = 1, Square = 2
25
26// we can also get the type name by using EnumHelper.GetName()
27Logger.Log($"square type is ${EnumHelper.GetName<Shapes>(squareValue)}");
28//> square type is Square

Example: Creating A Custom CardType

If your mod adds custom cards, you may also want to create custom types for some of them. Simply extend the CardType enum in your mods Awake method (see Awake or Ready?), so it runs before the cards are loaded.

1private void Awake()
2{
3  int customCardType = EnumHelper.ExtendEnum<CardType>("CustomCardType");
4  // use the variable if you need to check if a cards type is CustomCardType, do not hardcode the number!
5}
1{
2  "id": "examplemod_custom_card",
3  "nameOverride": "Custom Card",
4  "descriptionOverride": "a custom card with a custom type!",
5  "type": "CustomCardType"
6}