Pages

sábado, 21 de outubro de 2023

UEFN Verse: 1st version of round_based_device

In this article we will create the 1st version of the Verse device that will be responsible for controlling our game. When learning programming it is important to start with small blocks of code and try to understand all the elements that are being used.

Open the Verse Explorer, right-click on the project name and choose Add new Verse file to projectIn Device Name put round_based_device and click on Create button to use the template code.

If you don't know how to create a Verse file, I recommend reading this article from Part I:

Creating the first Verse device


Add a constant with the @editable attribute to reference the End Game device and an array to reference the Capture Areas as shown in the code below: 

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

round_based_device := class(creative_device):

    @editable
    EndGameDevice : end_game_device = end_game_device{}

    @editable 
    CaptureAreas: []capture_area_device = array{}


If you don't know what arrays are, read this article from Part I:

Storing Item Spawners in an Array


Create a function named AreaCaptured. This function will be executed when the player captures an area. For this to happen, we will subscribe the function in the AreaIsScoredEvent event of the Capture Area device. In this 1st version, the AreaCaptured function will only activate the End Game device that will end the round.

Remove the template code that is in the OnBegin function and add the code below:

    OnBegin<override>()<suspends>:void=
        for (Area : CaptureAreas):
            Area.AreaIsScoredEvent.Subscribe(AreaCaptured)

    AreaCaptured(Agent:agent):void=
        EndGameDevice.Activate(Agent)


In the OnBegin function we have a for loop that is used to iterate over the elements of the CaptureAreas array and subscribe the AreaCaptured function in the AreaIsScoredEvent of each of the Capture Areas.

The Verse code for the 1st version of round_based_device looks like this:

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

round_based_device := class(creative_device):

    @editable
    EndGameDevice : end_game_device = end_game_device{}

    @editable 
    CaptureAreas: []capture_area_device = array{}

    OnBegin<override>()<suspends>:void=
        for (Area : CaptureAreas):
            Area.AreaIsScoredEvent.Subscribe(AreaCaptured)

    AreaCaptured(Agent:agent):void=
        EndGameDevice.Activate(Agent)    


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, go to the Content Drawer and the CreativeDevices folder:


Drag the round_based_device and drop it onto the level at the location shown in the image below. This Verse device will also be useful as protection against guards' shots.



Select the round_based_device in the level. In the Details tab, select the EndGameDevice reference and add 5 elements to the CaptureAreas array. Select the Capture Area references.



Save the level and start the session. Capture one of the areas to end the current round. At the end of Round 5, the Game Winner screen will be displayed.



In the next article we will learn about the Round Settings device.


Table of Contents Verse