Hiro enables you to observe changes to the state of any systems used by using the SystemObserver helper or by subscribing an appropriate IObserver to that system. This can be useful for updating things such as UI in a reactive way.
The following example demonstrates how changes in the NakamaSystem can be observed.
Using the SystemObserver helper class allows you to bind an action to be triggered whenever the system values change.
1
2
3
4
5
vardisposer=SystemObserver<NakamaSystem>.Create(leaderboardsSystem,system=>{Instance.Logger.Info($"System updated.");// Update UI elements etc as necessary here...});
Alternatively you can implement the IObserver<T> interface. This approach gives you more control and also allows you to observe different system states, such as if the system encounters an error.
publicclassNakamaSystemObserver:IObserver<NakamaSystem>{publicNakamaSystemObserver(NakamaSystemnakamaSystem){nakamaSystem.Subscribe(this);}publicvoidOnCompleted(){Debug.Log("OnCompleted called");}publicvoidOnError(Exceptionerror){Debug.LogError($"OnError called: {error.Message}");}publicvoidOnNext(NakamaSystemnakamaSystem){Debug.Log($"NakamaSystem updated for user {nakamaSystem.Account.Username}");}}
Instantiate and use the class, for example in the GameCoordinator: