A class is a template that defines attributes and behaviors. When we create a Verse class, the editor provides us with some template options. These templates are classes that will be used as the parent class. The attributes and behaviors of the parent class will be inherited by the new class.
When we drag and drop a creative device onto the level, we are creating an instance of the creative device class. Each instance can have different values for its attributes. As an analogy, we are instances of a Person class. The Person class has the Name attribute, and each of us can have different names.
When creating a class we use constants and variables to specify the attributes. Behavior is defined using functions.
Besides the classes available as templates in UEFN, we can create simple classes in Verse. The example below creates a class named health_item with three constants of different types and a function called PrintData() that writes the values of the current instance to the screen.
health_item := class:
Name : string
HealthPoints : int = 10
Weight : float = 2.0
PrintData() : void =
Print("*** health_item instance ***")
Print("Name = {Name}")
Print("HealthPoints = {HealthPoints}")
Print("Weight = {Weight}")
Print("****************************")
The HealthPoints and Weight constants have default values that will be used if no other values are provided when creating the instance of this class.
The lines below are creating two instances of the health_item class. In the first instance, only the Name constant is being assigned a value. The other constants are using their default values. In the second instance, the three constants are receiving initialization values.
HealthItemInstance1 : health_item = health_item{Name := "Fruit"}
HealthItemInstance2 : health_item = health_item{Name := "Potion",
HealthPoints := 15,
Weight := 1.5}
HealthItemInstance1 and HealthItemInstance2 are references used to access the instances of the health_item class that have been created.
With a reference we can access the constants/variables and functions of a class using "." as shown in the example below.
HealthItemInstance1.PrintData()
HealthItemInstance2.PrintData()
if (HealthItemInstance1.Weight > HealthItemInstance2.Weight):
Print("{HealthItemInstance1.Name} is heavier than {HealthItemInstance2.Name}")
Let's create a device in UEFN to show how this new class can be used in the new device file.
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 class_instance_device and click the Create Empty button.Copy the Verse code below into the class_instance_device file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
health_item := class:
Name : string
HealthPoints : int = 10
Weight : float = 2.0
PrintData() : void =
Print("*** health_item instance ***")
Print("Name = {Name}")
Print("HealthPoints = {HealthPoints}")
Print("Weight = {Weight}")
Print("****************************")
class_instance_device := class(creative_device):
HealthItemInstance1 : health_item = health_item{Name := "Fruit"}
HealthItemInstance2 : health_item = health_item{Name := "Potion",
HealthPoints := 15,
Weight := 1.5}
OnBegin<override>()<suspends>:void=
HealthItemInstance1.PrintData()
HealthItemInstance2.PrintData()
if (HealthItemInstance1.Weight > HealthItemInstance2.Weight):
Print("{HealthItemInstance1.Name} is heavier than {HealthItemInstance2.Name}")
The class_instance_device has two attributes that are references to instances of the health_item class. In the OnBegin function, we call the PrintData() function of the instances to write the values of their attributes. Then, a comparison is made between the two instances using the Weight attribute.
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 class_instance_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.