Constants and variables represent memory locations used to store values.
A constant receives its value at initialization and this value cannot be modified during program execution.
A variable can have its value modified at any time.
The creation of constants and variables are similar. The only difference is in the use of the var keyword to indicate that it is a variable. This is the creation format:
var Identifier : type = initialization
Let's analyze each element from the line above:
- var : Used to indicate the creation of a variable. If omitted, it means that a constant is being created.
- Identifier : Name of the constant or variable.
- type : Indicates the type of values that can be stored.
- initialization : Expression used to generate an initial value for the constant or variable.
There are several types that can be used in constants and variables. The most common types are described below:
- logic : It can only store the Boolean values true or false.
- int : Stores integer values.
- float : It can store numerical values that are not integers.
- string : Used to store text (sequence of characters).
- Reference : Used to reference instances of a class. For example, a creative device that is in the level.
Identifier := initialization
Let's create a simple device in UEFN to see examples of using constants and variables.
In any UEFN project, open Verse Explorer, right-click on the project name and choose the Add new Verse file to project option.
Select the Verse Device template. In Device Name, put const_var_device and click the Create button to use the template code.
Modify the Verse code of the const_var_device device to have this content:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
const_var_device := class(creative_device):
# logic: Boolean values (true or false)
@editable
StartTimer : logic = false
# string: Stores text. "" is an empty string
@editable
Name : string = ""
# float: Accepts numeric values that are not integers
@editable
HeightInMeters : float = 1.80
# var: Indicates that it is a variable.
# int: Stores integer values.
var NameLength : int = 0
# This constant is a reference to an instance of the timer_device class
@editable
TimerDevice : timer_device = timer_device{}
# This function runs when the device starts in-game
OnBegin<override>()<suspends>:void=
# The string type of the WelcomeMsg constant was inferred from the expression.
# {Name} will be replaced by the value of the Name constant.
WelcomeMsg := "Welcome {Name} to UEFN"
# Use set to modify the value of a variable
set NameLength = Name.Length
# Print: It writes text on the screen and in the log
Print(WelcomeMsg)
Print("{Name} has {NameLength} characters")
# if: It executes a block of code if the result of the expression is success
if(StartTimer?):
TimerDevice.Start()
Lines starting with the # character are comments that are ignored by the Verse code.
@editable is a Verse language attribute that indicates that the constant or variable can be modified directly in UEFN.
Save the file and compile the Verse code using the Verse > Build Verse Code option from the UEFN menu.
To add our Verse device to the level, access the Content Drawer and the folder that has the project name plus Content.
Drag the const_var_device and drop it onto the level. Also drag onto the level a Timer Device which can be found in the Fortnite > Devices folder.
Select the const_var_device in the level. In the Details tab you can modify the values of the constants that were marked with @editable. Check the checkbox for the StartTimer constant. Enter your name in the Name constant. Click on the TimerDevice drop-down to select the Timer Device that we added 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. Also verify that the Timer Device is started by our Verse code if the StartTimer constant value is true.