The Enumeration (Enum) is used to create a new data type whose possible values are restricted to the set of constants defined in the enumeration.
In the example below, difficulty is an Enumeration with five possible values.
difficulty := enum{Story, Easy, Normal, Hard, Expert}
To define a difficulty type constant that can be modified in the editor, do this:
@editable
Difficulty : difficulty = difficulty.Normal
An Enumeration can be closed or open. The difficulty Enumeration we created is closed because it's the default. A closed Enumeration cannot be modified after the island (map or experience) has been published in Fortnite, so it should only be used when you're sure the Enumeration won't change.
To create an open Enumeration, use the <open> specifier as shown in the code below which defines some states that will be used by an NPC guard:
guard_states := enum<open>{Idle, Patrolling, Searching, Attacking}
Let's create a device in UEFN to demonstrate the use of Enumeration. In any UEFN project, open Verse Explorer, right-click on the project name and choose the Add new Verse file to project option.
In Device Name put enum_device and click the Create Empty button.
Copy the Verse code below into the enum_device file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
difficulty := enum{Story, Easy, Normal, Hard, Expert}
enum_device := class(creative_device):
@editable
Difficulty : difficulty = difficulty.Normal
var EnemyAttackModifier : float = 1.0
OnBegin<override>()<suspends>:void=
case(Difficulty):
difficulty.Story =>
set EnemyAttackModifier = 0.5
Print("Enemy Attack Modifier of 50% in Story Difficulty.")
difficulty.Easy =>
set EnemyAttackModifier = 0.75
Print("Enemy Attack Modifier of 75% in Easy Difficulty.")
difficulty.Normal =>
set EnemyAttackModifier = 1.0
Print("Enemy Attack Modifier of 100% in Normal Difficulty.")
difficulty.Hard =>
set EnemyAttackModifier = 1.25
Print("Enemy Attack Modifier of 125% in Hard Difficulty.")
difficulty.Expert =>
set EnemyAttackModifier = 1.5
Print("Enemy Attack Modifier of 150% in Expert Difficulty.")
This example will assign different values to the EnemyAttackModifier variable depending on the difficulty selected in enum_device.
Note that the case does not contain the default block "_". This is not required when the Enumeration used in the case is closed and all Enumeration values have code blocks.
Save the file and compile the Verse code using the Verse > Build Verse Code option from the UEFN menu.
Access the Content Drawer and add the enum_device to the level. Select the enum_device in the level and in the Details tab, click the Difficulty field dropdown to select one of the Enumeration values.
Save the level and click the Launch Session button to load it into Fortnite. After starting the Fortnite session, press the Tab key and select the Log tab to view the log with the message written according to the selected difficulty.