Offline Support #

By using the Unity SDK for Hiro, you are able to make use of the Offline Support feature. This will allow many Hiro systems to store state locally on the user’s device. Later on, when the device is able to connect to your Nakama backend, this locally stored state will be synced back to the server and cleared locally to free up any space that was being used.

Implementing Offline Support in your game doesn’t require any change to how you write your gameplay logic, we handle any local state management and syncing behind the scenes. All that is required from you is some simple configuration in your HiroCoordinator script that is demonstrated below.

Supported Systems #

Certain systems cannot function without a network connection due to their nature, such as Teams, which requires direct interaction with other users. Accordingly, only a subset of systems have Offline Support capability.

Here is a list of all the systems that have Offline Support:

Reachability Probes #

For your Unity client to be able to detect whether it is online or offline you must register at least one INetworkProbe. You can implement your own custom INetworkProbe if you have a very specific use case, however, we supply a few implementations out of the box that should handle the majority of use cases.

ALL registered network probes must determine that Online == true, otherwise offline mode will be entered. While only a small optimization, it is recommended to register your probes in this order if you are using more than one. This is in order of how easy/fast it is to be certain that there is no connectivity, and return early to save having to check the other probes.

ForceNetworkProbe #

Allows the developer to keep reference to the probe and set the Online property to false to manually enter offline mode. This can be useful to reduce the number of network calls when the user is playing parts of the game where being online isn’t necessary. The Online property can then be switched back to true when you want to sync and start receiving network updates again.

InternetReachabilityNetworkProbe #

Uses Unity’s Application.internetReachability static property to determine whether Wi-Fi/Cellular Data is enabled on the device or not. This does not guarantee internet reachability, but if this is turned off, we can be certain that internet reachability is impossible.

NakamaClientNetworkProbe #

Updates online state based on whether calls to your Nakama server have been successful recently. An interval for checking can be configured to reduce connectivity jitter when rapidly gaining and losing network connection. This is done by staying in offline mode for interval seconds, even when back online.

Unity Setup #

Here is how you would set up Offline Support in your HiroCoordinator script. Keep in mind that you don’t need to use all of the network probes shown here, this is just an example that uses them all together.

Alternatively, if you didn’t want your game to have Offline Support you would still need to create the NetworkMonitor, but you wouldn’t pass in any probes.

// Keep reference to manually change `Online` property when desired.
var forceProbe = new ForceNetworkProbe(true);
// Simply check `Application.internetReachability`.
var reachabilityProbe = InternetReachabilityNetworkProbe.Default
// Interval in seconds before trying to re-sync with Nakama after going offline.
var nakamaProbe = new NakamaClientNetworkProbe(TimeSpan.FromSeconds(60));

// Add the probes in the order you want them to be processed.
var monitor = new NetworkMonitor(forceProbe, reachabilityProbe, nakamaProbe);
// Detect when entering/exiting offline mode.
monitor.ConnectivityChanged += (_, args) =>
    Instance.Logger.InfoFormat($"Network is online: {args.Online}");

// Pass the nakamaProbe here to allow it to update when interacting with your Nakama server.
var nakamaSystem = new NakamaSystem(logger, scheme, host, port, serverKey, NakamaAuthorizerFunc(monitor), nakamaProbe);

// Remember to pass the NetworkMonitor to allow your systems to function while offline.
var systems = new Systems("HiroSystemsContainer", monitor, logger);