Pages

segunda-feira, 18 de novembro de 2024

UEFN Verse: if (control flow)

A program consists of a series of instructions. There are types of instructions that define when certain instructions should be executed. This defines the control flow of the program.

The if statement is widely used in control flow. The if tests one or more expressions that must result in success in order to execute the block of code associated with the if.

In the example below, GradeNumber and IntegerValue are variables. The Print() function will only be executed if the values ​​of the variables are different.

if (GradeNumber <> IntegerValue):
    Print("GradeNumber and IntegerValue are different")


If is an expression and can return value. It is also a failure context allowing the execution of failable expressions.

The example below shows the use of a single-line if statement. When the if test expression succeeds, the value of then is the result. When the expression fails, the value of else is the result. The result value is stored in the EvenOrOdd variable. 

set EvenOrOdd = if ( Mod[IntegerValue, 2] = 0) then "Even" else "Odd"
Print("IntegerValue is {EvenOrOdd}")

The Mod[] function returns the remainder of a division between integers. It is a failable expression. Failable functions use [ ] instead of ( ). In the example above, if the remainder of an integer divided by 2 is zero, then the number is even.

We can use an if expression after an else expression. This allows us to check other expressions until the successful expression is found.

In the example below, the if .. else if are used to define the scale of a grade using its numeric value. When one of the if .. else if expressions is successful, the others will be ignored. The resulting word will be stored in the GradeScale variable.  
set GradeScale = 
    if (GradeNumber >= 9):   
        "Excellent"
    else if (GradeNumber >= 7):
        "Good"
    else if (GradeNumber >= 5):
        "Average"
    else:
        "Insufficient"

Print("GradeScale is {GradeScale}")


The if expression has another format that is useful when there are multiple expressions that need to succeed for a block of code to execute. In this format, each if test expression can be written on one line, making the code more readable. The example below shows this format.

if: 
    GradeNumber > 0
    IntegerValue > 0
then: 
    Print("GradeNumber and IntegerValue are positive")

Let's create a device in UEFN to see the various forms of the if expression in action.

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

Copy the Verse code below into the if_device file:

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

if_device := class(creative_device):

    @editable
    GradeNumber : int = 8

    @editable
    IntegerValue : int = 3
    
    var EvenOrOdd : string = ""
    var GradeScale : string = ""

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

        if (GradeNumber <> IntegerValue):
            Print("GradeNumber and IntegerValue are different")

        set EvenOrOdd = if ( Mod[IntegerValue, 2] = 0) then "Even" else "Odd"
        Print("IntegerValue is {EvenOrOdd}")
    
        set GradeScale = 
            if (GradeNumber >= 9):   
                "Excellent"
            else if (GradeNumber >= 7):
                "Good"
            else if (GradeNumber >= 5):
                "Average"
            else:
                "Insufficient"

        Print("GradeScale is {GradeScale}")

        if: 
            GradeNumber > 0
            IntegerValue > 0
        then: 
            Print("GradeNumber and IntegerValue are positive")


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 Verse device operator_device to the level.

Select the if_device in the level. In the Details tab you can modify the values ​​of the constants that were marked with @editable.



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