Pages

sábado, 28 de outubro de 2023

UEFN Verse: Final version of round_based_device

This is the last article in Part II. Let's add the Verse code that controls the Guard Spawner devices.

The Guard Spawner device references will be stored in an array. Add the array declaration after the RoundDevice variables. 

    @editable
    GuardSpawners : []guard_spawner_device = array{}


Enabling Guard Spawner devices according to the current round is done in a similar way to Capture Area devices. Add the EnableGuardSpawners function below that receives as a parameter the number of Guard Spawners that must be enabled.

    EnableGuardSpawners(NumberOfSpawners:int):void=        

        MaxIndex := NumberOfSpawners - 1

        for (Index := 0..MaxIndex):
            if(GuardSpawner := GuardSpawners[Index]):
                GuardSpawner.Enable()                


The EnableGuardSpawners function will be called during round setup. It receives the CurrentRound variable as a parameter. The code below shows how the Round1Setup function will look like. Do the same for the other round setup functions.

    Round1Setup():void=
        set CurrentRound = 1
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)


With these changes we complete the Verse code for round_based_device. I decided to make a simple game to keep the focus on the round setup functions that are associated with the Round Settings devices. Within these functions you have countless possibilities for creating different rounds.

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

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{}

    var NumberOfAreasCaptured : int = 0

    var CurrentRound : int = 0

    @editable
    Round1Device : round_settings_device = round_settings_device{}

    @editable
    Round2Device : round_settings_device = round_settings_device{}

    @editable
    Round3Device : round_settings_device = round_settings_device{}

    @editable
    Round4Device : round_settings_device = round_settings_device{}

    @editable
    Round5Device : round_settings_device = round_settings_device{}

    @editable
    GuardSpawners : []guard_spawner_device = array{}

    OnBegin<override>()<suspends>:void=
        Round1Device.RoundBeginEvent.Subscribe(Round1Setup)
        Round2Device.RoundBeginEvent.Subscribe(Round2Setup)
        Round3Device.RoundBeginEvent.Subscribe(Round3Setup)
        Round4Device.RoundBeginEvent.Subscribe(Round4Setup)
        Round5Device.RoundBeginEvent.Subscribe(Round5Setup)

    AreaCaptured(Agent:agent):void=
        set NumberOfAreasCaptured += 1

        if( NumberOfAreasCaptured = CurrentRound):
            EndGameDevice.Activate(Agent)

    EnableCaptureAreas(NumberOfAreas:int):void=        
        MaxIndex := NumberOfAreas - 1

        for (Index := 0..MaxIndex):
            if(Area := CaptureAreas[Index]):
                Area.Enable()
                Area.AreaIsScoredEvent.Subscribe(AreaCaptured)

    EnableGuardSpawners(NumberOfSpawners:int):void=        

        MaxIndex := NumberOfSpawners - 1

        for (Index := 0..MaxIndex):
            if(GuardSpawner := GuardSpawners[Index]):
                GuardSpawner.Enable()                
            
    Round1Setup():void=
        set CurrentRound = 1
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)
                            
    Round2Setup():void=
        set CurrentRound = 2
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)

    Round3Setup():void=
        set CurrentRound = 3
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)
     
    Round4Setup():void=
        set CurrentRound = 4
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)

    Round5Setup():void=
        set CurrentRound = 5
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)
    


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

Select the round_based_device in the level. In the Details tab, add 5 elements to the GuardSpawners array and select the Guard Spawners device references:


Save the level and start the session. Each round, another Guard Spawner will be enabled, which will spawn 2 guards.



It will be difficult to complete the last rounds. I'll give you some tips:
  • Use the Verse device that is in the level to hide and protect yourself from gunfire.
  • When capturing an area, stay crouched and moving to avoid the guards' shots.
  • Try to eliminate one of the guards and run to get the rifle he drops.


I hope this example helps you in learning and practicing the Verse language. We still have a long way to go.


Table of Contents Verse