Creating Enumerations

Enumerations are a set of named constants with an underlying value associated with them. The name is represented by a String. The value can be represented by a value type. Enumerations are generated from an Enumeration Class file (SQF) when added to a config file with the file typ set as “enum” - as a result the variable holding the Type Definition will also state “enum” (e.g. XPS_BT_enum_Status) instead of “typ”. The file is preprocessed and built into a Type Definition the same as “typ” SQF files. However, to convert them to proper Enumerations, we will build them using the XPS_fnc_createEnumeration function instead of the createhashmap0bject command. This is because from one Type Definition file we create several actual instances as constants and tpyically only do this once.

The XPS_fnc_createEnumeration function creates two main things:

An example below inherits from the base class XPS_typ_Enumerations and sets a simple enumeration based on default incremental numbers.

Note: The type definition XPS_typ_Enumerations is not an “enum” as it in itself doesn't create any Enumerations. Think of it as an abstract class.

Another Note: XPS currently only supports ValueType to be set to Scalar (Number), String or Text (Structured Text). Might inclue future support for any type tht hashmaps support as keys.

Pets.sqf File:

[
	["#type","TAG_typ_Pets"],
  ["#base", XPS_typ_Enumeration], 
	["ValueType","SCALAR"],  // Currently Must be either SCALAR, STRING, or TEXT (default is "SCALAR" if not a supported type.
	["Enumerations", [ "None", "Cat", "Dog", "Bird"]]
] 

If we call, for example in a preInit function, the following:

["TAG_Pets", TAG_enum_Pets ] call XPS_fnc_createEnumeration; 

We will end up with the following hashmap objects assigned to the associated global variables:

TAG_Pets - the helper class

Key Value Type Example Syntax
#str "TAG_typ_Pets" String str TAG_Pets
#type ["TAG_typ_Pets", "XPS_typ_Enumeration"] Array TAG_Pets get “#type”
Names [ “None”, "Cat", "Dog", "Bird"] Array TAG_Pets get “Names”
Values [ 0 , 1 , 2 , 3 ] Array TAG_Pets get “Values”
None {TAG_Pets_None} Code TAG_Pets call ["None"]
Cat {TAG_Pets_Cat} Code TAG_Pets call ["Cat"]
Dog {TAG_Pets_Dog} Code TAG_Pets call ["Dog"]
Bird {TAG_Pets_Bird} Code TAG_Pets call ["Bird"]
0 {TAG_Pets_None} Code TAG_Pets call [0]
1 {TAG_Pets_Cat} Code TAG_Pets call [1]
2 {TAG_Pets_Dog} Code TAG_Pets call [2]
3 {TAG_Pets_Bird} Code TAG_Pets call [3]
GetEnum { Method to retrieve the global variable 'constant' } Code e.g. TAG_Pets call [”GetEnum”, “Cat”] -or- TAG_Pets call [”GetEnum”, 1]
IsDefined { Method to determine if Name or Value exists } Code e.g. TAG_Pets call [”IsDefined”, “Dog”] -or- TAG_Pets call [”IsDefined”, 2]

TAG_Pets_None

#str “None”
#type [“TAG_Pets_None”,”TAG_Pets”]
Value 0

TAG_Pets_Dog

#str “Dog”
#type [“TAG_Pets_Dog”,”TAG_Pets”]
Value 2

TAG_Pets_Cat

#str “Cat”
#type [“TAG_Pets_Cat”,”TAG_Pets”]
Value 1

TAG_Pets_Bird

#str “Bird”
#type [“TAG_Pets_Bird”,”TAG_Pets”]
Value 3

For the purpose of type / value checking, these conditions will be true:

Tag_Pets_None isEqualType createhashmap // All enumerations are (of course) hashmap objects
Tag_Pets_None isEqualType Tag_Pets_Dog // Same as above
!(Tag_Pets_None isEqualRef Tag_Pets_Dog) // All enumerations are natively seperate instances
[Tag_Pets_None, Tag_Pets_Cat] call XPS_fnc_isEqualHashmapObjectType // both derive from Tag_Pets type defintion
Tag_Pets_None isEqualTo "None"

What can we do with Enumerations?

// Getting an Enumeration by name or value from helper class
private _myPet = TAG_Pets call [2]; // Dog - 2

// Getting an Enumeration by name or value - nil safe alternative
private _myOtherPet = TAG_Pets call ["GetEnum","Cat"];  // Cat -1 or False if not Defined

// Getting an Enumeration by variable
private _myNeighborsPet = TAG_Pets_Bird;  // Bird - 3

// Getting an Enumeration's name or value
private _myPetValue = _myPet get "Value"; // 2
private _myPetName = str _myPet;  // "Dog"

// Comparing Enumerations
private _areSame = _myPet isEqualTo _myOtherPet; // False
private _isDog = TAG_Pets_Dog isEqualRef _mypet;  // True
private _areAnyBird = TAG_Pets_Bird in [_mypet, _myOtherPet]; // False

_myPet = Tag_Pets call [selectrandom [1,2,3]];
switch (_myPet) do {
	case TAG_Pets_Dog : {
		hint "I am a good boy!!"
	};
	case TAG_Pets_Cat : {
		hint "I am a jerk."
	};
	case TAG_Pets_Bird : {
		hint "Chirp!!"
	};
};