sábado, 7 de dezembro de 2024

UEFN Verse: Functions

A function is a block of code that can be executed in different parts of a program. A function is a simple way to reuse code.

For example, imagine that your game has a help message that can be executed in various situations, such as when entering a new environment or when the player presses a button. If we do not use a function, the block of code with this help message will have to be repeated in several places in the program. When there is a change in the text of the message, we will have to search for all the places in the code that have this message, with the risk of forgetting one of the places and the message becoming inconsistent.

We can create a function to display the help message. In the places of the code that need the message, we will place a call to the function. When we need to change the text of the message, we only need to change it in one place, which is inside the function.

The code below shows the creation of a function named ShowHelp:

ShowHelp():void = 
    Print("ShowHelp() function has no parameter or result.")

This is a very simple function that displays a message on the screen using the Verse language's Print() function. The void keyword indicates that the function does not return a result value.

A function can have parameters and return a result. A parameter is a value that is passed to the function. The function below has two integer parameters named Level and TimeLeft and the result is also of integer type. The function uses an expression with the parameters and the result of the expression is returned to the location where the function was called.

CalculateBonus(Level:int, TimeLeft:int):int =
    Level * 10 + TimeLeft


UEFN creative devices generate various types of events to indicate what is happening in the game. We can register a function on a device to be executed when a specific event occurs.

The function below was created to be called when a Timer Device finishes its countdown. 

HandleTimerFinished(MaybeAgent:?agent):void=
   Print("*** Time's Up! ***")

The function that will be registered in an event must have the parameters and return type specified in the event. In general, these functions do not have a return type (void type) and have a parameter of type agent or ?agent.

Note: ?agent is an optional agent. This means it can have a value or be empty.

The line below subscribes the HandleTimerFinished function on the success event of a TimerDevice:

TimerDevice.SuccessEvent.Subscribe(HandleTimerFinished)


Let's put the above examples together on a device in UEFN to see how they work in practice.

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

Copy the Verse code below into the function_device file:

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

function_device := class(creative_device):

    @editable
    TimerDevice : timer_device = timer_device{}

    ShowHelp():void = 
        Print("ShowHelp() function has no parameter or result.")
        

    CalculateBonus(Level:int, TimeLeft:int):int =
        Level * 10 + TimeLeft


    HandleTimerFinished(MaybeAgent:?agent):void=
       Print("*** Time's Up! ***")
        

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

        TimerDevice.SuccessEvent.Subscribe(HandleTimerFinished)

        ShowHelp()

        Bonus1 := CalculateBonus(3, 25)
        Print("Bonus 1 = {Bonus1}")

        Print("Bonus 2 = { CalculateBonus(6, 12) }")


The OnBegin function makes calls to the functions that were created and subscribes the HandleTimerFinished function in the success event of a Timer Device, which will be executed when the Timer Device finishes its countdown.

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 function_device to the level. Also drag onto the level a Timer Device which can be found in the Fortnite > Devices folder.

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