Enum is short for enumeration, which means to name or list a series of things, called enumerators. This is a type in Verse that can be used for things like days of the week or compass directions.
Click image to enlarge.
Closed and Open Enums
Verse uses the <open> and <closed> attribute specifiers on enums to determine how you can change the definition of the enum once your island is published.
Enums are closed by default. With closed enums, you cannot add or reorder enum values or change a closed enum to an open one once your island has been published.
Closed enums are best used for cases where your values are expected to stay the same, like days of the week.
With open enums, you can:
Add new enum values.
Reorder enum values.
Change an open enum to a closed enum.
Open enums are best used when you expect the number of values in your enum may increase in the future. For example, an enum of weapon types.
Open enums cannot be used in a case statement without a default case. Closed enums can be used in case statements without a default case only if all enumeration values have a case.
Creating an Enum
| Action | Code Example |
|---|---|
Creating closed enums: Enums are closed by default. Use the keyword You can explicitly define the enum as closed by including the | Verse |
Creating open enums: You must explicitly define an open enum by including the | Verse |
Accessing an enumerator: Use | Verse |
Persistable Type
An enum is persistable when it is defined with the <persistable> specifier. This means that you can use them in your module-scoped weak_map variables and have their values persist across game sessions.
For more details on persistence in Verse, check out Using Persistable Data in Verse.
Non-persistent enums cannot be used with persistable data.
The following is an example of a closed persistable enum for the days of the week that can be stored, updated, and accessed later for a player.
day := enum<persistable>:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
SundayIf not specified, all enums are closed by default.
Published Enums
Once you have published your island, certain aspects of closed and open enums with the <persistable> specifier are fixed.
Closed enums:
Cannot be updated to become
<open>.Cannot add, rename, reorder or remove enum values.
If not specified, you can add the
<closed>specifier.
Open enums:
Can be updated to become a closed enum with the
<closed>specifier.Can add and reorder enum values.
Cannot rename or remove enum values.
Cannot be used in case statements without a default case.