segunda-feira, 17 de fevereiro de 2025

UEFN Verse: Map

A Map allows the storage of key->value pairs. This is used to make associations between values. The example below creates a Map variable using string for the key type and int (integer) for the value type.

var HealthItemSizes : [string]int = map{"small" => 25, "medium" => 50, "big" => 75, "mega" => 100}

To get a value associated with a key in a Map, use the key inside [] as shown in the example below. Accessing a value in a Map is a failable expression.

if(Points := HealthItemSizes["medium"]):
    Print("The medium Health Item recovers {Points} health points.")

You can add elements to a Map variable using set with a new key. If the key used already exists in the Map, the value associated with this key will be updated.

if(set HealthItemSizes["ultra"] = 150):
    Print("Size ultra added to the map.")

The number of elements in a Map can be accessed using the Length property.

Print("Number of elements in the Map: {HealthItemSizes.Length}")

The For loop can be used to iterate through the elements of a Map as shown in the following example. At each iteration of the For loop, the key and value of an element in the Map are stored in local constants.

for (Key->Value : HealthItemSizes):
    Print("Size: {Key} | Points: {Value}")

Let's create a device in UEFN to demonstrate these Map concepts. 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 map_device and click the Create Empty button.

Copy the Verse code below into the map_device file:

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

map_device := class(creative_device):

    @editable
    HealthExtended : logic = true

    var HealthItemSizes : [string]int = map{"small" => 25, "medium" => 50, "big" => 75, "mega" => 100}

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

        if(HealthExtended?):        
            if(set HealthItemSizes["ultra"] = 150, set HealthItemSizes["monster"] = 200):
                Print("Health Extended elements added to the map.")
            
        Print("The medium Health Item recovers {GetHealthItemPoints("medium")} health points.")
        
        Print("------- MAP CONTENTS -------")

        for (Key->Value : HealthItemSizes):
            Print("Size: {Key} | Points: {Value}")

        Print("Number of elements in the Map: {HealthItemSizes.Length}")

    GetHealthItemPoints(Size:string):int=

        if(Points := HealthItemSizes[Size]):
            Points
        else:
            0

    # The return keyword is optional. The function returns the result of the last executed expression.


The HealthExtended constant can be edited in the UEFN editor. It is being used to add other elements to the Map.

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 map_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.