quinta-feira, 19 de março de 2026

UEFN Verse: Extension Methods

Methods are functions that belong to a class, just as fields or attributes represent class data, such as variables and constants. This terminology comes from Object-Oriented Programming.

The Verse language allows the creation of extension methods, which are functions that can be added to existing classes without the need for inheritance.

This can be useful when you need to add functionality to classes that cannot be extended, such as player, agent, and team in UEFN.

In addition to classes, extension methods can be used with any type in Verse. The example below adds a function to the int type that calculates the power of a number.

(Base:int).MyPowFunction(Exponent:int):int=
    if (Exponent < 0):
        return 0
    var PowResult: int = 1
    for (Ind := 1..Exponent):
        set PowResult *= Base
    PowResult

Inside the parentheses is a constant of the type being extended. This constant is used in the function body as if it were a parameter.

The following code shows how to use the function:

IntValue := 2
Result := IntValue.MyPowFunction(4)


We will use extension methods to add functionality to the player class in UEFN. To do this, a new class and a map will be used to associate additional fields with a player instance.

Open Verse Explorer, right-click on the project name, and select the option Add new Verse file to project.

In Device Name put extension_device and click the Create Empty button.

Copy the Verse code below into the extension_device file:

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

player_fields := class:
    var Score : int = 0
    var CoinsCollected : int = 0
    var GameLevel : int = 1
    var CoinsPerLevel : int = 3    

extension_device := class(creative_device):

    @editable
    ItemSpawnerArray : []item_spawner_device = array{}

    var PlayerFieldsMap<public> : [player]player_fields  = map{}

    OnBegin<override>()<suspends>:void =
        for(ItemSpawner : ItemSpawnerArray):
            ItemSpawner.ItemPickedUpEvent.Subscribe(HandleCoinCollected)

    HandleCoinCollected(Agent:agent):void=
        if ( Player := player[Agent], Player.CollectCoin[] ) {}

    (Player:player).GetPlayerFields()<decides><transacts>:player_fields=
        if(PlayerFields := PlayerFieldsMap[Player]):
            PlayerFields
        else:
            NewPlayerFields := player_fields{}
            set PlayerFieldsMap[Player] = NewPlayerFields
            NewPlayerFields

    (Player:player).CollectCoin()<decides><transacts>:void=
        PlayerFields := Player.GetPlayerFields[]
        set PlayerFields.CoinsCollected += 1
        set PlayerFields.Score += PlayerFields.GameLevel * 100
        if ( Mod[PlayerFields.CoinsCollected, PlayerFields.CoinsPerLevel] = 0 ):
            set PlayerFields.GameLevel += 1
        Print("Score:{PlayerFields.Score} | GameLevel:{PlayerFields.GameLevel}")

The player_fields class contains the new fields that will be associated with a player instance through the PlayerFieldsMap map.

The extension_device contains an array of item_spawner_device used to store references to the Item Spawners in the level. The OnBegin function registers HandleCoinCollected on each instance in the item_spawner_device array so it is executed when the player collects a coin.

The HandleCoinCollected function receives the agent that collected the coin as a parameter. It is cast to player, and then the CollectCoin extension method created for the player class is called. The if expression is used only as a failure context.

GetPlayerFields is an extension method created for the player class. It returns the player_fields instance associated with the player. If it does not exist yet, a new player_fields instance is created, stored in the map, and returned by the method.

CollectCoin is another extension method for the player class. It retrieves the player_fields instance to update its values. In this example, GameLevel increases every three coins collected, and the score awarded for each coin depends on it.

Save the file and compile the Verse code using the Verse > Compile Verse Code option from the UEFN menu.

Access the Content Drawer and add the constructor_device to the level. Add an Item Spawner Device to the level and, in the Details tab, configure it as shown in the image so that a coin is generated every 3 seconds.


Make copies of the Item Spawner Device by holding Alt and dragging one of the axis arrows on the selected device. In my example, there are four Item Spawners.


Select the extension_device at the level. In the Details tab, add elements to the array and select the Item Spawner Device references for each element.


Save the level and click the Launch Session button to load the level in Fortnite. When a coin is collected, a message is displayed showing the player's current Score and GameLevel.