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 player’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.

Offline Support only works on devices that have connected your Nakama server at least once, so that the client can pull configurations from the server. Once the first connection has been made, the game can be played offline by using the latest data from the server. Future updates to the game will only be seen once the game client connects to the server for a sync.

Supported Systems #

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

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

Even these systems that have offline capabilities might not have 100% support for all their features. For example, the Economy system has Offline Support, the player can gain soft currency while offline, but can’t purchase hard currency due to requiring an online connection to process the online payments.

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 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// 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);

Syncing Objects #

On the Client #

When the game client regains an online connection, it automatically syncs any local state deltas back to the server. However, it is also possible to call the Sync function manually.

1
await _systems.SyncAsync();

On the Server #

When each client submits their sync data, the server will process the offline state deltas, then return the entire up to date server state.