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}")
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.