segunda-feira, 8 de dezembro de 2025

UEFN Verse: Implementing an interface

An interface is a composite type, just like a class, that is used to specify what a class can do without specifying how it will be done. Let's analyze the interface example below.

challengeable := interface:

    IsChallengeCompleted():logic

This code defines the challengeable interface, which has the IsChallengeCompleted() function. Note that the function has no body, as it must be implemented by the class that implements the interface. This example has only one function, but the interface can have as many functions as needed.

Using an interface allows the creation of a type of protocol or contract that can be followed by various classes of different types. Somewhere in the project code, we can handle objects that implement the challengeable interface without needing to know the actual type of the class.

In Verse, implementing an interface is done in the same way as class inheritance. A Verse class can only have one parent class, but it can inherit (implement) multiple interfaces.

In the code below, the trigger_manager_device class inherits from the creative_device class and implements the challengeable interface. It needs to override the IsChallengeCompleted function of the interface.

trigger_manager_device := class(creative_device, challengeable):

    var IsAllTriggersActivated : logic = false

    IsChallengeCompleted<override>():logic=
        IsAllTriggersActivated
		
    ...

Each class that implements the challengeable interface will have its own rules for defining the result of the IsChallengeCompleted() function. The class above considers the challenge completed when all triggers are activated. We could have another class that performs some kind of puzzle using buttons.

The interface type can be used in object reference type conversion, also known as type cast. For example, CreativeDevice is a reference of type creative_device and therefore can point to an instance of any subclass of creative_device.

The code below tests whether the CreativeDevice reference points to an instance of a class that implements the challengeable interface. If the cast works, ChallengeableDevice will store a reference of type challengeable.

if( ChallengeableDevice := challengeable[ CreativeDevice ] ):

	IsChallengeCompleted := ChallengeableDevice.IsChallengeCompleted()

	...

Let's look at an example to illustrate the use of the interface with the trigger_manager_device that implements the challengeable interface.

This device will have 3 trigger_devices. When all trigger_devices are activated, the variable IsAllTriggersActivated receives the value true. The value of this variable is returned in the IsChallengeCompleted() function.

Open Verse Explorer, right-click on the project name and choose the Add new Verse file to project option.

In Device Name put trigger_manager_device and click the Create Empty  button.

Let's create the challengeable interface in this same file. Copy the Verse code below into the trigger_manager_device file:

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

challengeable := interface:

    IsChallengeCompleted():logic

trigger_manager_device := class(creative_device, challengeable):

    var IsAllTriggersActivated : logic = false

    @editable
    Trigger1 : trigger_device = trigger_device{}

    @editable
    Trigger2 : trigger_device = trigger_device{}

    @editable
    Trigger3 : trigger_device = trigger_device{}

    OnBegin<override>()<suspends>:void=
        sync:
            Trigger1.TriggeredEvent.Await()
            Trigger2.TriggeredEvent.Await()
            Trigger3.TriggeredEvent.Await()

        set IsAllTriggersActivated = true   

    IsChallengeCompleted<override>():logic=
        IsAllTriggersActivated

In the OnBegin function, the sync expression is used to wait until all triggers are activated.

Save the file and compile the Verse code using the Verse > Compile Verse Code option from the UEFN menu.

Let's create another device that will use the IsChallengeCompleted() function from the challengeable interface to check if the challenge has been completed and activate the end_game_device.

In Verse Explorer, right-click the project name and choose the option Add new Verse file to project.

In Device Name, put interface_device and click the Create Empty button.

Copy the Verse code below into the interface_device file:

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

interface_device := class(creative_device):
    
    @editable
    CreativeDevice : creative_device = creative_device{}

    @editable
    EndGameDevice : end_game_device = end_game_device{}
    
    OnBegin<override>()<suspends>:void=

        if( ChallengeableDevice := challengeable[ CreativeDevice ] ):

            loop:
                IsChallengeCompleted := ChallengeableDevice.IsChallengeCompleted()

                if: 
                    IsChallengeCompleted?
                    PlayerAgent := GetPlayspace().GetPlayers()[0]
                then:
                    EndGameDevice.Activate(PlayerAgent)
                    break   #end loop

                Sleep(0.5)
        else:
            Print("The device doesn't implement the challengeable interface!")        

We are using a reference to creative_device, which is the parent class of creative devices. Therefore, this example will work for any type of device that implements the challengeable interface.

The OnBegin() function checks if we have an instance that implements the challengeable interface. Then a loop is started that checks every 0.5 seconds if the challenge has been completed.

Save the file and compile the Verse code using the Verse > Compile Verse Code option from the UEFN menu.

Access the Content Drawer and add the trigger_manager_device and interface_device to the level.

Add one End Game Device and three Trigger Devices to the level. The image below shows the devices used in this example.


Select the trigger_manager_device at the level. In the Details tab, select the 3 instances of Trigger Device.


Select the interface_device at the level. In the Details tab, add the references for the trigger manager device and the End Game Device.


Save the level and click the Launch Session button to load the level in Fortnite. After activating the 3 triggers, the interface_device will detect that the challenge has been completed and will trigger the end of the match.