Along with facilitating the implementation of powerful Nakama-based features into your game, Hiro can also be used to help you structure your own game systems and logic.
This page demonstrate two different custom systems to showcase how you can use this functionality: the first example makes use of an existing Hiro system while the second is not tied to existing systems at all.
The second example makes use of Hiro’s deterministic startup to ensure that it runs after another system has been initialized and then bootstraps the gameplay logic.
publicclassGameBootstrapSystem:IInitializeSystem{publicstringName=>nameof(FirstTimePlayerSystem);publicboolIsInitialized{get;privateset;}privateNakamaSystem_nakamaSystem;publicGameBootstrapSystem(NakamaSystemnakamaSystem){_nakamaSystem=nakamaSystem;}publicasyncTaskInitializeAsync(){if(!_nakamaSystem.IsInitialized){returnTask.FromException(newInvalidOperationException("NakamaSystem is not initialized"));}GameManager.Instance.Initialize(_nakamaSystem);GameManager.Instance.StartGame();}}
While the order of initialization is deterministic, it is important to ensure that systems you are dependent on have initialized successfully.
In the above scenario:
The IsInitialized property of the NakamaSystem is checked to ensure that the system was fully initialized before continuing.
If not, the InitializeAsync call returns with an Exception wrapped in a Task.
This is then handled by the Systems object which will call the system’s InitializeFailedAsync method.
You can handle this two ways:
If the initialization failure of this system is critical to your game, you can re-throw the exception from within InitializeFailedAsync.
This causes the main Systems object to halt the initialization flow, stopping any further systems from being initialized.
1
2
3
4
5
6
7
publicasyncTaskInitializeFailedAsync(ILoggerlogger,Exceptione){logger.Error($"GameBootstrapSystem failed to initialize, halting further initialization.\n{e.Message}");// Re-throw the exception, causing the Systems object to halt further initializationreturnTask.FromException(e);}
You can choose to allow initialization of further systems to continue by returning a completed task.
1
2
3
4
5
6
7
publicasyncTaskInitializeFailedAsync(ILoggerlogger,Exceptione){logger.Error($"GameBootstrapSystem failed to initialize, continuing initialization of other systems.\n{e.Message}");// Return a completed task to allow further systems to be initializedreturnTask.CompletedTask;}