Answer the question
In order to leave comments, you need to log in
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;
}
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;
}
}
private void OnEnable()
{
_player.StatsInteractor.OnCoinsValueChanged += RenderText;
}
private void OnDisable()
{
_player.StatsInteractor.OnCoinsValueChanged -= RenderText;
}
public CharacterStats CharacterStats;
public CharacterStatsInteractor StatsInteractor;
private void Awake()
{
StatsInteractor = new CharacterStatsInteractor(CharacterStats);
}
Answer the question
In order to leave comments, you need to log in
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 RenderText
call after subscribing and don't suffer.
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;
EventManager::addListener("coin_changed", RenderText);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question