Class specifiers are used when creating classes to define behaviors and characteristics. This article will present the main ones.
The <abstract> specifier is used to create superclasses with common fields and methods to be inherited by subclasses.
An abstract class allows the declaration of methods without a body (it has no code) that must be overridden in a subclass.
The code below creates the abstract class base_bonus. The ApplyBonus() method has no body.
base_bonus := class<abstract>():
BonusId : int
Name : string
BonusValue: int
BonusData(): string =
"BonusId: {BonusId} | Name: {Name} | BonusValue: {BonusValue}"
ApplyBonus() : void
It is not possible to create instances of an abstract class. The code below generates a compilation error.
BaseBonus : base_bonus = base_bonus{BonusValue := 20, Name := "Generic"}
The <concrete> specifier indicates that all fields of the class must have default values, allowing the creation of instances with an empty archetype.
The code below creates the concrete class score_bonus using base_bonus as the superclass. All fields have been given default values and the ApplyBonus() method has been overridden.
score_bonus := class<concrete>(base_bonus):
BonusId<override> : int = 2
Name<override> : string = "Score MEDIUM"
BonusValue<override>: int = 50
ApplyBonus<override>() : void =
Print("Adding {BonusValue} to SCORE.")
This code creates an instance of score_bonus using an empty archetype {}.
ScoreBonusDefault : score_bonus = score_bonus{}
The <final> specifier is used to indicate that a class cannot have subclasses. This specifier can also be used on fields and methods to indicate that they cannot be overridden.
The code below creates the final class time_bonus using base_bonus as the superclass.
time_bonus := class<final>(base_bonus):
BonusId<override> : int = 1
ApplyBonus<override>() : void =
Print("Adding {BonusValue} to TIME.")
The new time_bonus class cannot be used as a superclass. The following code generates a compilation error.
time_bonus_variation := class(time_bonus):
Let's combine these examples into a single file so we can test it on UEFN. I created a verse device called class_specifiers_device and used this code:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
base_bonus := class<abstract>():
BonusId : int
Name : string
BonusValue: int
BonusData(): string =
"BonusId: {BonusId} | Name: {Name} | BonusValue: {BonusValue}"
ApplyBonus() : void
time_bonus := class<final>(base_bonus):
BonusId<override> : int = 1
ApplyBonus<override>() : void =
Print("Adding {BonusValue} to TIME.")
score_bonus := class<concrete>(base_bonus):
BonusId<override> : int = 2
Name<override> : string = "Score MEDIUM"
BonusValue<override>: int = 50
ApplyBonus<override>() : void =
Print("Adding {BonusValue} to SCORE.")
class_specifiers_device := class(creative_device):
TimeBonus : time_bonus = time_bonus{BonusValue := 30, Name := "Time MEDIUM"}
ScoreBonusDefault : score_bonus = score_bonus{}
OnBegin<override>()<suspends>:void=
Print( ScoreBonusDefault.BonusData() )
ScoreBonusDefault.ApplyBonus()
This Verse device simply executes the two methods of the ScoreBonusDefault instance to write some messages to the log. The device was created to illustrate the use of class specifiers.