quinta-feira, 13 de fevereiro de 2025

UEFN Verse: Option

In the Verse language there is a type called Option that allows a variable to have a value or be empty.

An Option is defined by using "?" in front of the type. The line below defines a variable called MaybeGrade which is an Option of integer type.

var MaybeGrade : ?int = false

The false indicates this Option is empty. It is common to start the name of Option type variables with Maybe because it already indicates that it can have a value or be empty.

You might be thinking that we could have just used the value 0 in the variable to indicate that it has no value. However, this can be confusing. How do we know if the value 0 indicates that a grade has not yet been assigned or if the grade value was actually 0. 

The following code shows how to assign a value to an Option:

set MaybeGrade = option{ 8 }

Inside the { } you can have any type of expression. If the expression fails, the Option will be false indicating that it is empty.

To access the value stored in an Option, you must use the ? operator within a failable context (such as if).

if (Grade := MaybeGrade?):
    Print("Grade = {Grade}")

The Print will only be executed if MaybeGrade has a value.

Let's create a device in UEFN with an example of using Option. This example will simulate a challenge. Imagine an obstacle course. The agents (players) have a limited amount of time to overcome the obstacles and press a button. 

We'll use an Option variable named MaybeBestAgent to store the first agent who presses the button. If no agent presses the button before the timer runs out, the Option MaybeBestAgent will remain empty.

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

Copy the Verse code below into the option_device file:

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

option_device := class(creative_device):

    var MaybeBestAgent : ?agent = false

    @editable
    Button : button_device = button_device{}

    @editable
    TimerDevice : timer_device = timer_device{}

    @editable
    ScoreManagerDevice : score_manager_device = score_manager_device{}
    
    OnBegin<override>()<suspends>:void=

        Button.InteractedWithEvent.Subscribe(ButtonPressed)
        
        TimerDevice.SuccessEvent.Subscribe(HandleTimerFinished)

    ButtonPressed(Agent:agent):void=    

        # Stores the first agent to press the button
        if ( not MaybeBestAgent? ):
            set MaybeBestAgent = option{ Agent }

    HandleTimerFinished(MaybeAgent:?agent):void=
    
    # Checks if any agent managed to press the button before the timer ends
        if(BestAgent := MaybeBestAgent?):
            ScoreManagerDevice.Activate(BestAgent)
        else:
            Print("Nobody won the challenge!")

The button_device, timer_device and score_manager_device are being used. 

The ButtonPressed() function will be executed when an agent presses the button_device. It will store the agent if the Option MaybeBestAgent is empty.

The HandleTimerFinished() function will be executed when the Timer ends. If the Option MaybeBestAgent has an agent, he will receive a score.

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 option_device to the level. Also add the button_device, timer_device, and score_manager_device. Remember, we are just simulating the challenge. 

Select the Score Manager in the level. In the Details tab, set the Score Value that the winner of the challenge will receive.


Select the Timer Device in the level. In the Details tab, set the Duration in seconds of the timer. Check Start at Game Start to start the Timer automatically.


Finally select our Option Device in the level. In the Details tab add the references for the devices we are going to use.


Save the level and click the Launch Session button to load the level into Fortnite. You can press the button to receive your score or wait for the timer to finish and see a message that nobody won the challenge.


Table of Contents Verse