U
U
Uncle Bogdan2021-09-28 16:34:00
Unity
Uncle Bogdan, 2021-09-28 16:34:00

How to call an event when everyone has already subscribed?

There is a CharacterStats class with Character statistics:

[System.Serializable]
public class CharacterStats
{
    public float MaxHealth;
    public float Health;
    public int Coins;
}

There is a CharacterStatsInteractor class that handles actions with the repository:

public class CharacterStatsInteractor
{
    private CharacterStats _characterStats;

    public int Coins => _characterStats.Coins;

    public delegate void OnCoinsValue();
    public event OnCoinsValue OnCoinsValueChanged;

    public CharacterStatsInteractor(CharacterStats repository)
    {
        _characterStats = repository;

        OnCoinsValueChanged?.Invoke();
    }

    public void AddCoins(int value)
    {
        _characterStats.Coins += value;

        OnCoinsValueChanged?.Invoke();
    }

    public void SpentCoins(int value)
    {
        _characterStats.Coins -= value;

        OnCoinsValueChanged?.Invoke();
    }

    public void TakeDamage(int value)
    {
        _characterStats.Health -= 0;
    }
}

Then I signed up for the event:

private void OnEnable()
    {
        _player.StatsInteractor.OnCoinsValueChanged += RenderText;
    }

    private void OnDisable()
    {
        _player.StatsInteractor.OnCoinsValueChanged -= RenderText;
    }

Then I created a character and wrote this:

public CharacterStats CharacterStats;
    public CharacterStatsInteractor StatsInteractor;

    private void Awake()
    {
        StatsInteractor = new CharacterStatsInteractor(CharacterStats);
    }

Everything seems to be fine, but the event does not work.
I understand that it does not work due to the fact that the event is called before it is signed.

I hope no one writes that you just need to change Awake to Start, since the character script is disabled from the very beginning.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2021-09-28
@freeExec

Your own logic is flawed.
- call an event in the constructor
- get a reference to the newly created object
- subscribe to the event
- ??? where is the profit?
Just RenderTextcall after subscribing and don't suffer.

Z
zukrac, 2021-10-07
@zukrac

1) Either normally wait for the next operation. Coins after all will be updated again?
2) Either organize this moment yourself, the global event "tha_gama_is_started" and trigger OnCoinsValueChanged
on it 3) Or make the record for the event available before the creation of the object itself (if appropriate)
, for example, through a static variable

public static event OnCoinsValue OnCoinsValueChanged;

4) Or make an event manager object so that it is singleton / static, so that it is created automatically when you try to sign up for an event. And everyone signed himself in his OnEnable
EventManager::addListener("coin_changed", RenderText);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question