Pages

domingo, 6 de julho de 2025

UEFN Verse: Access Specifiers

Access specifiers are used to define the visibility of elements in Verse code. They can be used on classes, fields, methods, and other elements.

The <public> specifier indicates that the element is available anywhere in the code.

The <protected> specifier indicates that the element is available in the class itself and in its subclasses.

The <private> specifier indicates that the element is available within the class itself but subclasses cannot access it.

When no access specifier is used, the element uses the default <internal> specifier which indicates that the element is available in the module it is in. The next article will be about modules.

The code below is an example of a class that was created just to show the application of access specifiers in the class, fields and method. 

weapon := class<public>():

    var<protected> ammunition<public>: int = 10

    calibration<private> : float = 0.03	

    Fire<public>(): void =
        if (ammunition > 0):
            set ammunition -= 1 
            Print("Weapon fired")
        else:
            Print("No ammo")

In variables, the specifier used next to var indicates where it can be modified. The specifier used next to the variable name indicates where it can be accessed. In the example above, the variable ammunition can be accessed anywhere but can only be modified within the class itself and its subclasses.

Consider WeaponInstance an instance of the weapon class that is being used inside another class. The following accesses are valid because the elements are <public>:

Print("{WeaponInstance.ammunition}")
WeaponInstance.Fire()

The accesses below are invalid because calibration is <private> and the var of ammunition is <protected>:

Print("{WeaponInstance.calibration}")
set WeaponInstance.ammunition = 15

The code below creates the rifle_grenade subclass using weapon as the superclass. The grenades variable is using the default access specifier which is <internal>.

rifle_grenade := class<public>(weapon):

    var grenades: int = 3

    LaunchGrenade<public>():void =
        if (grenades > 0):
            set grenades -= 1 
            Print("Grenade launched")
        else:
            Print("No grenade")

    RifleStatus<public>():void =
        Print("ammunition: {ammunition} | grenades: {grenades}")

In the RifleStatus() method we can see that the ammunition variable of the weapon superclass is accessible. It can also be modified in the subclass.

The calibration variable is not accessible in the rifle_grenade subclass because it is <private>.

The code below shows these example classes in a single file so you can test them on UEFN. I created a verse device called access_specifiers_device and used this code:

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

weapon := class<public>():

    var<protected> ammunition<public>: int = 10

    calibration<private> : float = 0.03	

    Fire<public>(): void =
        if (ammunition > 0):
            set ammunition -= 1 
            Print("Weapon fired")
        else:
            Print("No ammo")

rifle_grenade := class<public>(weapon):

    var grenades: int = 3

    LaunchGrenade<public>():void =
        if (grenades > 0):
            set grenades -= 1 
            Print("Grenade launched")
        else:
            Print("No grenade")

    RifleStatus<public>():void =
        Print("ammunition: {ammunition} | grenades: {grenades}")

access_specifiers_device := class(creative_device):

    RifleGrenade : rifle_grenade = rifle_grenade{}

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

        Print("{RifleGrenade.ammunition}")
        RifleGrenade.Fire()
        RifleGrenade.LaunchGrenade()
        RifleGrenade.RifleStatus()

This Verse device creates an instance of the rifle_grenade class and accesses the ammunition variable and the Fire() method of the weapon superclass and two more methods of the rifle_grenade class.