Enum - MSUTeam/MSU GitHub Wiki
File Path: /msu/classes/enum.nut
An MSU Enum is different from a standard squirrel Enum in that it is extendable after declaration, and is better than a simple table (like those used in vanilla Battle Brothers) in that it enforces a specific order and allows for keys to be easily added after a table is created without having to keep track of things like COUNT
. Of course, it also has a variety of utility functions and allows iteration.
local msuEnum = ::MSU.Class.Enum( _array = null )
// _array is an array
If _array
is null
, the Enum initializes empty, if it isn't, after initialization the Enum calls this.addArray(_array)
<Enum>.addArray( _array )
// _array is an array
Iterates over each element in _array
and calls this.add(element)
with each one.
<Enum>.add( _key )
// _key is a string
_key
must be a unique (to this enum), non-empty, capitalized string.
Adds _key
to the keys in the enum, assigning it a unique integer that can be accessed with
<Enum>[_key]
or using dot notation as normal
<Enum>.getKeyForValue( _value )
// _value is an integer
_value
must be more than or equal to 0 and less than len()
Returns the Key for the _value
passed.
<Enum>.len()
Returns the number of identifiers in the Enum.
<Enum>.contains( _value )
// _value is an integer
Returns true
if the value is in the enum, and false
otherwise.
local myEnum = ::MSU.Class.Enum([
"First",
"Second"
]);
::logInfo(myEnum.First) // prints 0
::logInfo(myEnum["Second"]) // prints 1
::logInfo(myEnum.Third) // errors
myEnum.add("Third");
::logInfo(myEnum.Third) // prints 2
::logInfo(myEnum.getKeyForValue(myEnum.First)) // prints "First"
::logInfo(myEnum.getKeyForValue(0)) // prints "First"
::logInfo(myEnum.len()) // prints 3
::logInfo(myEnum.contains(myEnum.Second)) // prints true
::logInfo(myEnum.contains(20)) // prints false
foreach (key, value in myEnum)
::logInfo(key + " | " + value)
// this would print out
// "First | 0"
// "Second | 1"
// "Third | 2"