Pages

segunda-feira, 19 de junho de 2023

Getting objects with tags in Verse

In the previous article, we created the tag coin_spawner to identify the Item Spawners that are at the level. In this article we will use the GetCreativeObjectsWithTag function using the coin_spawner tag as parameter to get the Item Spawners references.

In Unreal Engine, objects that can be added to the level are known as Actors.

We will create a function named SetupCoinSpawnerDevices that will fill the CoinSpawnerDevices array with references to the Item Spawners of the level.

The return of the GetCreativeObjectsWithTag function will be stored in the TaggedActors array.  It will not be stored directly in the CoinSpawnerDevices array because we need to ensure that the Actor that received the coin_spawner tag is really an Item Spawner

This is the code for function SetupCoinSpawnerDevices:

    SetupCoinSpawnerDevices():void=

        TaggedActors := GetCreativeObjectsWithTag(coin_spawner{})

        set CoinSpawnerDevices = for:
            TaggedActor : TaggedActors
            ItemDevice := item_spawner_device[TaggedActor]
        do:
            ItemDevice

        SpawnCoin() 

The CoinSpawnerDevices array will be filled with the result of the for loop.  Each iteration of the for loop adds an ItemDevice if the Cast from TaggedActor to item_spawner_device does not fail.  This is the line where the Cast is done:

            ItemDevice := item_spawner_device[TaggedActor]

TaggedActor is the element of the TaggedActors array that is being used in the current iteration of the for loop.  The TaggedActor is a reference of type actor which is a superclass of item_spawner_device.

We use Cast to convert the actor reference to item_spawner_device. If TaggedActor is not of the item_spawner_device class then the Cast will fail and the block of do: will not run on the current iteration, then the current element will not be added in the CoinSpawnerDevices array.

At the end of the  SetupCoinSpawnerDevices function, after filling the CoinSpawnerDevices array, it is being called our SpawnCoin function to generate the coin in one of the Item Spawners of the array.

In the OnBegin function, remove the SpawnCoin() and put the function  SetupCoinSpawnerDevices():

    OnBegin<override>()<suspends>:void=
        TimerDevice.SuccessEvent.Subscribe(HandleTimerFinished)
        SetupCoinSpawnerDevices()


After the changes in this article, the Verse code of collect_game_device should look like this:

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

coin_spawner := class(tag){}

collect_game_device := class(creative_device):

    @editable
    TimerDevice : timer_device = timer_device{}

    @editable
    EndGameDevice : end_game_device = end_game_device{}

    @editable
    var CoinSpawnerDevices : []item_spawner_device = array{}

    OnBegin<override>()<suspends>:void=
        TimerDevice.SuccessEvent.Subscribe(HandleTimerFinished)
        SetupCoinSpawnerDevices()
        
    HandleTimerFinished(MaybeAgent:?agent):void=
        if(Agent := MaybeAgent?):
            EndGameDevice.Activate(Agent)

    SpawnCoin():void=    
        if: 
            IndexSpawner: int = GetRandomInt(0, CoinSpawnerDevices.Length - 1)
            NextCoinSpawner := CoinSpawnerDevices[IndexSpawner]
        then:
            NextCoinSpawner.SpawnItem()

    SetupCoinSpawnerDevices():void=

        TaggedActors := GetCreativeObjectsWithTag(coin_spawner{})

        set CoinSpawnerDevices = for:
            TaggedActor : TaggedActors
            ItemDevice := item_spawner_device[TaggedActor]
        do:
            ItemDevice

        SpawnCoin()

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

Each time the game is started, the coin will be randomly generated at one of the Item Spawners.


Table of Contents Verse