quinta-feira, 15 de junho de 2023

Storing Item Spawners in an Array

In this article we are going to create an array in our Verse device to store references to the Item Spawners that are in the level. 

An array is a container that stores elements of the same type. Access to elements of an array is done using an index.


Add the CoinSpawnerDevices array definition after the EndGameDevice definition: 

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

We're using @editable to test the array in this article by adding an element directly in UEFN. In the final version, the @editable can be removed because we will fill the array using Verse code.

Note that the line in the CoinSpawnerDevices array definition starts with var. This means that CoinSpawnerDevices is a variable and not a constant. We need to use var because CoinSpawnerDevices will be modified later.

The [] in front of the type is what defines that the variable is an array. This array will be used to store item_spawner_device type references. The array{} constructor is initializing the array.

Let's create a function named SpawnCoin that will use a random index to select one of the Item Spawners that are stored in the array to generate the coin.

In order to use the functions that generate random numbers, we need to include the Random module at the top of the file: 

using { /Verse.org/Random }

Add the SpawnCoin function at the end of the file. Use 4-space indentation so the function belongs to the collect_game_device class:

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

The return type of the SpawnCoin function is void which means that the function does not return a value.

In this function a different form of the if statement is being used. Each line below the if must execute successfully for the then to execute.

The if and then statements are being used to ensure that NextCoinSpawner has a valid reference to an Item Spawner.

The GetRandomInt function is being used to generate a random integer value that we will use as an array index to reference one of the Item Spawners that are in the level. The index of the first element of an array is 0 and the index of the last element is the length of the array minus 1.

The value of the IndexSpawner constant is used to access the Item Spawner reference that is in the array at this position. The Item Spawner reference is stored in the NextCoinSpawner constant. It was not necessary to specify the type item_spawner_device between the : and = because the compiler can infer the type from the result of the expression.

The item_spawner_device class has a function called SpawnItem to generate the item using Verse code.

Let's call our SpawnCoin function when starting the game. Add the SpawnCoin() line into the OnBegin function:

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

The Verse code below shows how the collect_game_device class will look like with the changes of this article:

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


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)
        SpawnCoin()
        
    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()

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

Select the collect game device that is on the level.  See that the CoinSpawnerDevices array appears on the Details tab.  The array appears in UEFN because we use @editable.

To add an item to the array, click the + button, then click the Index [ 0 ] drop-down and select Item Spawner.  This is the Item Spawner reference we placed in the level.


Save the level and start the session.  Note that a coin will appear on the Item Spawner that is in the level.  This coin was generated by our Verse code.  If we already had several Item Spawners in the level, only one of them would be randomly chosen to generate the coin.

We still need to create a tag and apply it to the Item Spawner before making the other copies of the Item Spawner.  This will be the topic of the next article.


Table of Contents Verse