segunda-feira, 4 de novembro de 2024

UEFN Verse: Operators

Operators are symbols that represent arithmetic, relational, or logical operations.


  • Arithmetic Operators

Arithmetic operators perform mathematical operations between two values.

These are the main arithmetic operators:

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division


The line below shows an example use of an arithmetic operator. Consider that Damage, AttackHit, and ShieldLevel are variables. The value of ShieldLevel is subtracted from the value of AttackHit. The result is stored in the Damage variable:

    set Damage = AttackHit - ShieldLevel


Arithmetic operators can be combined with the assignment operator to modify the value of a variable.

For example, in the expression below, we are adding the initial value of the Damage variable with the value of the DamageAmplifier variable and storing the result in the Damage variable:

    set Damage = Damage + DamageAmplifier


This same expression can be rewritten using the += operator like this:

    set Damage += DamageAmplifier


Note 1: In the Verse language there are failable expressions. This means that the expression needs to be executed in a failure context, such as the if expression. 

Note 2: Division between integers is failable. You must use the Floor() function to get the integer part of the result or the Ceil() function to round the result up.

Note 3: The Verse language does not have the % (modulo) operator used to get the remainder of a division between integers. Instead you can use the Mod[] function.


  • Relational Operators

Relational (or comparison) operators are used to compare two values. The result of the comparison will be either success or failure.

Note this important difference between the Verse language and other languages ​​that use Boolean values ​​(true or false) as the result of comparison. Failure is an element of the Verse language used to control program flow.

Expressions that use relational operators are failable.


These are the relational operators:

Operator Comparison
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
= Equal to
< > Not equal to


The example below checks if the value of the PlayerHealth variable is less than or equal to zero. If the if statement succeeds then the GameOver() function will be executed.

    if (PlayerHealth <= 0): 

        GameOver()


  • Logical Operators

Logical (or decision) operators perform operations with failable expressions (the result of each expression can be success or failure).

These are the logical operators:

Operator Decision
or Success if one of the expressions is success
and Success if both expressions are success
not It uses only one expression.
The result is the opposite value


Before showing an example of a logical operator, let's learn about another operator in the Verse language.


  • ? (query)

The ? operator is called query and is used to check whether a value of type logic is true resulting in success. The expression fails if the logic value is false.

The example below shows the use of the ? operator and the logical and operator. The variable HasKey is of type logic and the variable EnemyCount is of type int. The if expression will succeed if HasKey has the value true and EnemyCount is equal to zero.

    if (HasKey? and EnemyCount = 0):

        OpenDoor()


  • Operator precedence

Operators have an order of precedence. This means that operators with higher precedence are executed first in an expression.

The list below shows the precedence of operators. 

Higher precedence
?
not
*  /
+  -
=  < >  <  <=  >  >=
and
or
set  =


When two operators have the same precedence in an expression, the operator on the left will be executed first.


  • Grouping expressions

You can group expressions using parentheses ( ) to define the order in which the expressions are executed. Expressions enclosed in ( ) take precedence over operators and must be executed first.

As an example, see the two lines below:

    set Result1 = 5 * 2 + 4

    set Result2 = 5 * (2 + 4)

In the first line, the variable Result1 will receive the value of 14. The multiplication will be performed first because the * operator has higher precedence than the + operator.

In the second line, the variable Result2 will receive the value of 30. The expression inside the parentheses will be executed first. Then the multiplication will be performed.


Let's create a device in UEFN to see the operators 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 operator_device and click the Create Empty button.

Copy the Verse code below into the operator_device file:

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

operator_device := class(creative_device):
        
    X : float = 6.0
    Y : float = 2.0

    LogicTrue : logic = true
    LogicFalse : logic = false

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

        Print("X = {X} --- Y = {Y}")

        Print("X + Y = {X + Y}")
        Print("X - Y = {X - Y}")
        Print("X * Y = {X * Y}")
        Print("X / Y = {X / Y}")

        Print("X > Y  --- { if(X > Y) then "Success" else "Failure"} ")
        Print("X <= Y --- { if(X <= Y) then "Success" else "Failure"} ")
        Print("X = Y  --- { if(X = Y) then "Success" else "Failure"} ")
        Print("X <> Y --- { if(X <> Y) then "Success" else "Failure"} ")

        Print("LogicTrue? or LogicFalse? --- { 
                if(LogicTrue? or LogicFalse?) then "Success" else "Failure"} ")
                                               
        Print("LogicTrue? and LogicFalse? --- { 
                if(LogicTrue? and LogicFalse?) then "Success" else "Failure"} ")

        Print("not LogicFalse? --- { 
                if(not LogicFalse?) then "Success" else "Failure"} ")


The Print function allows the inclusion of an expression within the characters { }. The result of the expression will be part of the text that will be written.

In the Print functions that use relational and logical operators, if statements were used to test the success or failure of the expressions and return these results as text.

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.

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