GameData version4

GameData wtih Custom Unity Events:

This version of GameData has 2 custom UnityEvents included:

The code snippet below shows that the events are declared as type UnityEvent, they will be used in PlayerStats classes so that changes in playerData: score or health, or changes in the will be updated

    public UnityEvent onPlayerDataUpdate;

The code below shows that when TakeDamage is executed, there is a call to the InvokePlayerDataUpdate( ) method. The InvokePlayerDataUpdate( ) method checks to see if there are any subscribed 'listeners', such that the event is not null, then it Invokes the event which executes all methods that have been added as listeners.

//updated TakeDamage: 
    public void TakeDamage(int value)
    {
        health -= value;
        if (health < 0) health = 0;  //makes sure health !< 0
        Debug.Log("Health is updated " + health);
        InvokePlayerDataUpdate();
    }

    //Execute custom event if there are listeners
    public void InvokePlayerDataUpdate()
    {
        if (onPlayerDataUpdate != null)
        {
            onPlayerDataUpdate.Invoke();
        }
    }

Last updated