Array is a container used to store a group of elements of the same type. An array is defined using [] in front of the type. The example below shows the definition of an array named BonusByLevel to store integer values. The array is already initialized with some values.
BonusByLevel : []int = array{10, 25, 50, 75, 100}
Access to the elements of an array is done by an index that indicates the position of the element. The first element of an array is stored at index 0. The array has the Length property that stores the number of elements in the array.
The code below gets the value that is at index 1 of the array and stores it in the constant Value. The result written on the screen will be 25.
if ( Value := BonusByLevel[1] ):
Print("{Value}")
Accessing an element in an array is a failable expression, which is why it is being done inside if().
Once an array is initialized, its values cannot be modified. To change the values stored in an array, you need to create an array variable as shown in this example:
var BonusVariable : []int = array{50, 100, 150}
if ( set BonusVariable[1] = 120 ) {}
Arrays are very useful for grouping devices and performing operations on them. Let's create a device in UEFN that has an array of customizable_light_device. This device allows the use of Verse code to turn the light on or off.
Our device will have a reference to a button_device. When the player interacts with the button, the current light will be turned off and the next one will be turned on.
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 array_device and click the Create Empty button.
Copy the Verse code below into the array_device file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
array_device := class(creative_device):
var LampIndex : int = 0
@editable
LampsArray : []customizable_light_device = array{}
@editable
Button : button_device = button_device{}
OnBegin<override>()<suspends>:void=
Button.InteractedWithEvent.Subscribe(ChangeLamp)
ChangeLamp(Agent:agent):void=
if( CurrentLamp := LampsArray[LampIndex] ):
CurrentLamp.TurnOff()
set LampIndex += 1
if( LampIndex >= LampsArray.Length ):
set LampIndex = 0
if( NextLamp := LampsArray[LampIndex] ):
NextLamp.TurnOn()
The LampsArray has the @editable attribute that allows editing the array in the UEFN editor. The ChangeLamp function is subscribed in the InteractedWithEvent event of the button. It will be executed when the player interacts with the button.
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 our array_device to the level. We will also need a button_device and some customizable_light_device (in my example I used 4). I suggest choosing different colors to make the change of lights more visible.
Select the array_device in the level. In the Details tab you can add elements to the LampsArray by clicking the + button. Select a customizable_light_device reference for each element in the array. Also select the reference for the button_device.
Save the level and click the Launch Session button to load the level into Fortnite. Interact with the button device to toggle the lights.