sexta-feira, 6 de junho de 2025

UEFN Verse: Introduction to specifiers

Specifiers are used in the Verse language to define behaviors for some Verse elements. The symbols < and > are used by specifiers. The OnBegin function, present in Verse devices, has two specifiers: override and suspends. 

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

The <override> specifier indicates that the OnBegin function is overriding the OnBegin function of the parent class creative_device. We will look at the <override> specifier in more detail in an article on class inheritance.

The <suspends> specifier is used to define asynchronous functions that execute in parallel. This means that they can be suspended and then resumed to continue their execution.

Another common specifier is <decides> used to create failable expressions. You can generate a failure inside the function by using the expression false? .

If you create a function using only the <decides> specifier, when you try to call the function you will get a compilation error with the following message:

"This invocation calls a function that has the 'no_rollback' effect, which is not allowed by its context."

To avoid this error, you can use the <transacts> specifier which indicates that expressions executed inside the function can be rolled back.

The line below shows an example of a function declaration using <decides> and <transacts>:

    IncreaseExperience(Points: int)<decides><transacts>:void=


Let's create a device in UEFN applying these specifiers. 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 specifiers_device and click the Create Empty button.

This device will call the IncreaseExperience function once per second, passing in a different value that will be added to a variable called ExperiencePoints. There is a 1 in 6 chance that the IncreaseExperience function will fail. If it fails, the ExperiencePoints variable will roll back its value.

Copy the Verse code below into the specifiers_device file:

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

specifiers_device := class(creative_device):
    
    var ExperiencePoints : int = 0

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

        var XpGain : int = 0
        
        loop:
            set XpGain += 1

            if( IncreaseExperience[XpGain] ):
                Print("XpGain is valid")
            else:
                Print("*** XpGain is INVALID ***") 
                Print("ExperiencePoints rolled back to {ExperiencePoints}")

            Sleep(1.0)

    IncreaseExperience(Points: int)<decides><transacts>:void=

        set ExperiencePoints += Points
        Print("ExperiencePoints set to {ExperiencePoints}")

        if( GetRandomInt(1, 6) = 1 ):
            false?   #this is a failure

Note that in the OnBegin function, a loop is being used without a break or return expression to exit the loop. The loop repeats the expressions in its code block, and the Sleep() function suspends the OnBegin function for a second.

The IncreaseExperience function is a failable function due to the use of <decides>, so we use [ ] instead of ( ) in the call.

The GetRandomInt() function generates a random number between two numbers. The numbers used as parameters can be part of the result. To use this function, you need to include the Verse.org/Random module at the beginning of the file.

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 specifiers_device to the level.

Click the Launch Session button located in the UEFN toolbar to load the level into Fortnite. 

After starting the session in Fortnite, press the Tab key and select the Log tab to view the log with the messages written by the Print functions.


Table of Contents Verse