segunda-feira, 1 de setembro de 2025

UEFN Verse: Case (Control Flow)

Case is a control flow expression that compares a value to a list of options to decide which block of code should be executed.

In the example below, Mode is a string variable. There are code blocks associated with the values ​​"EASY", "NORMAL", and "HARD". The _ symbol represents the default block that will be executed if the Mode value has no associated block.

case (Mode):
    "EASY" =>
        set NumberOfEnemies = 15
        Print("Easy mode selected")
    "NORMAL" => 
        set NumberOfEnemies = 25
        Print("Normal mode selected")
    "HARD" =>
        set NumberOfEnemies = 35
        Print("Hard mode selected")
    _ => 
        Print("Mode is invalid")

A expressão usada no teste do Case pode ser dos tipos int, logic, string, char e enum. No artigo sobre enum (enumeração) veremos como usar o Case com enum.

The expression used in the Case test can be of the int, logic, string, char, or enum types. In the article on enum (enumeration), we'll see how to use Case with enum.

To demonstrate how to use the Case, we'll create a device that will function as a fortune message generator. The device will have references to a button device and a billboard device. When the player presses the button, one of the messages will be selected and displayed on the billboard.

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

Copy the Verse code below into the case_device file:

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

case_device := class(creative_device):

    @editable
    BillboardDevice : billboard_device = billboard_device{}

    @editable
    ButtonDevice : button_device = button_device{}

    var IndexOfMessage : int = 0
    var FortuneString : string = ""

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

        loop:
            ButtonDevice.InteractedWithEvent.Await()

            set IndexOfMessage = GetRandomInt(1, 5)

            set FortuneString = case (IndexOfMessage):
                1 => "A journey of a thousand miles begins with a single step."
                2 => "One bad chapter doesn't mean your story is over."
                3 => "It is easier to stay out than to get out."
                4 => "Experience is the best teacher."
                5 => "Try and fail, but never fail to try."
                _ => "Message not found."

            BillboardDevice.SetText( ConvertToMessage(FortuneString) )
            Sleep(2.0)

    ConvertToMessage<localizes>(StringValue:string) : message = "{StringValue}"

The OnBegin function contains a loop. The first expression within the loop waits until the player interacts with the button. After interacting with the button, a random number from 1 to 5 is generated and stored in IndexOfMessage to be used as a test expression in the case.

Each case block results in a string that will be stored in the FortuneString variable. The BillboardDevice's SetText() function takes a message as a parameter, so it was necessary to create the ConvertToMessage() function to convert from a string to a message.

The Sleep() function is used to pause for 2 seconds before restarting the loop and waiting again for the player to press the button to see another message.

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

Add a Button Device and a Billboard Device to the level. Select the Billboard Device and in the Details tab, check the Show Border field to make the billboard visible on the level.

Select the case_device in the level. In the Details tab, add the Billboard Device and Button Device references.



Save the level and click the Launch Session button to load the level into Fortnite. Interact with the button to display one of the messages on the Billboard. Press again to see another message.