terça-feira, 20 de junho de 2023

Score Manager device

In this article we are going to use a device called Score Manager to assign points to the player when he collects a coin.

Access o Content Drawer and the folder CollectGame Content > Fortnite > Devices. Search for score manager: 


Drag the Score Manager and drop it onto the level next to the Verse device.

In the Details tab, User Options category, we have the Score Value and Score Award Type properties that define how the score are given to the player when the device is activated. Keep these properties with the default values of 1 and Add:


Check the Display Score Update on HUD property so that a message is displayed when the player receives score. Also check the Reset HUD Message Score property so that the message on the screen only shows the score of the last coin collected.


Now let's create the reference to Score Manage in the Verse device code. Add the ScoreManagerDevice constant below the line of the CoinSpawnerDevices array:


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

We'll create a function named HandleCoinPickedUp to register at the Item Pickup event of the Item Spawners. This function will be called when the player collects a coin. 

    HandleCoinPickedUp(Agent:agent):void=

        ScoreManagerDevice.Activate(Agent)

        SpawnCoin()

The HandleCoinPickedUp function activates the Score Manager device which will assign points to the player and then calls the SpawnCoin function to generate another coin. In the next article we will add scoring rules, time bonus and game level in this function.

We need to register the HandleCoinPickedUp function on all Item Spawners in the level. This will be done in the SetupCoinSpawnerDevices function after filling the CoinSpawnerDevices array. A for loop is used to iterate through the CoinSpawnerDevices array and register the HandleCoinPickedUp function on each of the array's elements.

    SetupCoinSpawnerDevices():void=

        TaggedActors := GetCreativeObjectsWithTag(coin_spawner{})

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

        for (SpawnerDevice : CoinSpawnerDevices):
            SpawnerDevice.ItemPickedUpEvent.Subscribe(HandleCoinPickedUp)

        SpawnCoin()
    


The new version of collect_game_device will look like this:

# v0.5
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{}

    @editable
    ScoreManagerDevice : score_manager_device = score_manager_device{}

    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

        for (SpawnerDevice : CoinSpawnerDevices):
            SpawnerDevice.ItemPickedUpEvent.Subscribe(HandleCoinPickedUp)

        SpawnCoin()
    
    HandleCoinPickedUp(Agent:agent):void=

        ScoreManagerDevice.Activate(Agent)

        SpawnCoin()

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. On the Details tab, click on the ScoreManagerDevice drop-down and select Score Manager: 


Save the level and start the session. By collecting a coin you will earn a point and another coin will be generated.

In the next article we will make some improvements related to score and time and finish this mini game.


Table of Contents Verse