sexta-feira, 8 de agosto de 2025

UEFN Verse: Race (Time Flow)

Race is one of the main time flow control expressions in the Verse language, as it allows a clean way to abort other asynchronous code.

The race expression executes other expressions concurrently and terminates as soon as one of the expressions in its block completes. The other expressions within the race are aborted.

The code below shows a simple example of the race expression:

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

        race:
            ItemSpawner1.ItemPickedUpEvent.Await()
            ItemSpawner2.ItemPickedUpEvent.Await()
            ItemSpawner3.ItemPickedUpEvent.Await()
            
        Print("One of the items was collected")

ItemSpawner1ItemSpawner2 and ItemSpawner3 are instances of item_spawner_device. The Await() function waits until the event is triggered. The race expression ends when one of the items is collected.

To exemplify the use of the race expression, we will create a minigame. The objective of the minigame is to collect a certain number of coins within the specified time. These values will be stored in editable fields so they can be configured on the Verse device.

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

Copy the Verse code below into the race_device file:

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

race_device := class(creative_device):

    @editable
    ItemSpawner1: item_spawner_device = item_spawner_device{} 

    @editable
    ItemSpawner2: item_spawner_device = item_spawner_device{} 

    @editable
    ItemSpawner3: item_spawner_device = item_spawner_device{} 

    @editable 
    ChallengeTimeInSeconds : float = 60.0

    @editable
    NumberOfItemsToWin : int = 10

    var ItemCount : int = 0

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

        race:
            loop:
                race:
                    ItemSpawner1.ItemPickedUpEvent.Await()
                    ItemSpawner2.ItemPickedUpEvent.Await()
                    ItemSpawner3.ItemPickedUpEvent.Await()

                set ItemCount += 1
                Print("Items Collected: {ItemCount}")

                if(ItemCount >= NumberOfItemsToWin):
                    Print("WIN: Goal achieved")
                    break

            block:
                Sleep(ChallengeTimeInSeconds)
                Print("LOSE: Time expired!")

This device contains references to three item_spawner_devices. These devices will generate coins for the player to collect.

The race expression is being used twice in the OnBegin function. The first race has two concurrently expressions:

  • loopRepeats the expressions in its code block until interrupted by a break expression or aborted. In the example, the loop counts the coins collected and ends when the number of coins stored in NumberOfItemsToWin is reached.
  • blockIt's used to group expressions. Expressions within the block are treated as a single expression for the race. In the example, the block has a Sleep function that will wait until the challenge timer runs out.

The first race will end when the player reaches the objective or when time runs out.

The second race is within the loop code block. It completes when one of the coins is collected. The rest of the loop code executes, and the race is executed again if the loop is still active.

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 race_device to the level. Also add three Item Spawner Device and place them far from each other.



In each Item Spawner Device, add an item to the Item List and in Pickup to Spawn select the Gold item. For Time Before First Spawn, set it to 0.0 so the item appears in the level when you start the game. For Time Between Spawns, set different values for each device. In my example, I used 5.0s, 10.0s, and 15.0s.


Select the race_device in the level. In the Details tab, add the Item Spawner Device references. You can also modify the values used in the minigame challenge.


Save the level and click the Launch Session button to load the level into Fortnite. Collect the required amount of coins before time runs out to win the challenge.