segunda-feira, 3 de fevereiro de 2025

UEFN Verse: For loop

The For loop is used to repeat a block of code a specified number of times.

The code below is a very simple example of using the For loop to write the numbers from 1 to 5 on the screen.

for ( Number := 1..5 ):
    Print("{Number}")

The expression Number := 1..5 is a generator that creates a sequence of integers that starts at 1 and ends at 5. The number of values in the sequence will determine the number of times the For loop will execute the code block. For each execution, the current value of the sequence will be stored in Number to be used in the For code block.

The For loop is often used to iterate through the elements of an Array. The following example writes to the screen all the elements of an Array of integers.

Values : []int = array{4,2,9,6,3}
for ( X : Values ):
    Print("{X}")

At each iteration of the For loop, one of the elements of the Array is stored in X.

It is possible to know the index of the Array element that is being used in the current iteration of the For loop as shown in this code:

Values : []int = array{4,2,9,6,3}
for ( X->Y: Values ):
    Print("Index = {X}, Value = {Y}")

Let's create a device in UEFN to show different ways of using the For loop. Our device has 3 main features:

  • Turns on or off a group of lamps;
  • Calculates the factorial of a number;
  • Finds the index of the largest value stored in an array;

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 for_device and click the Create Empty button.

Copy the Verse code below into the for_device file:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

for_device := class(creative_device):

    @editable
    FactorialValue : int = 8

    @editable
    ArrayOfIntegers : []int = array{4,2,9,6,3}

    @editable
    LampsArray : []customizable_light_device = array{}

    @editable
    Sensor : volume_device = volume_device{}

    OnBegin<override>()<suspends>:void=

        Sensor.AgentEntersEvent.Subscribe(TurnOnLamps)

        Sensor.AgentExitsEvent.Subscribe(TurnOffLamps)
        
        FactorialResult := CalculateFactorial( FactorialValue )
        Print("Factorial of {FactorialValue} is {FactorialResult}")

        IndexResult := IndexOfHighestValue(ArrayOfIntegers)
        Print("Index of the highest value of the array: {IndexResult}")

    TurnOnLamps(Agent:agent):void=
        for (CurrentLamp : LampsArray):
            CurrentLamp.TurnOn()

    TurnOffLamps(Agent:agent):void=
        for (CurrentLamp : LampsArray):
            CurrentLamp.TurnOff()

    CalculateFactorial(Number:int):int=

        var FactorialResult : int = 1

        for (X := 1..Number):
            set FactorialResult = FactorialResult * X

        return FactorialResult

    IndexOfHighestValue(ValuesArray: []int):int=

        var IndexHighest : int = 0
        var HighestValue : int = 0 

        for ( CurrentIndex -> Value : ValuesArray):
            if( Value > HighestValue ):
                set HighestValue = Value
                set IndexHighest = CurrentIndex 

        return IndexHighest

A Volume Device is being used to detect when an Agent (the player) enters or exits a volume. The lights are turned on when entering the volume and turned off when exiting the volume.

The CalculateFactorial and IndexOfHighestValue functions show other ways to use the For loop.

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 for_device to the level. We will need 4 customizable_light_device and a Volume Device. Place the Volume Device near the customizable_light_device to make it easier to see.


Select the for_device in the level. In the Details tab, add the elements to the LampsArray and select the reference to the Volume Device that will be used as the Sensor. You can also change the default values ​​that are being used in the FactorialValue constant and the ArrayOfIntegers array.


Save the level and click the Launch Session button to load the level into Fortnite. Move your character to the location of the Volume Device to turn on the lights, then move away to turn them off.

Press the Tab key and select the Log tab to view the log with the messages written by the CalculateFactorial and IndexOfHighestValue functions.


Table of Contents Verse