Best practices for offline/online game

Hey guys, I’ve added a number of features to my game lately that require internet connectivity: ads, cloud save, IAP, and google’s game network among them. 

This means my game needs to intelligently handle situations like being opened with an internet connection, and then taken into the subway where there is no internet connection. Or the reverse: started offline, and taken out of the subway.

And as far as I can tell, I need to wrap each of those libraries in a network check, because if I don’t they can do stupid things like take 30 seconds to timeout from a blocking network call. Literally, my game takes 30 seconds to get past the splash screen right now if there’s no net connection, and it’s instantaneous with a connection. I’m still tracking down the culprit, and if you have any tips on tracking this sort of thing down please let me know. Maybe somewhere in Android Debug Monitor?

Anyway, because Android doesn’t support network.canDetectNetworkStatusChanges I’ve had to implement a sketchy version using sockets (calling home to google.com), which blocks execution of the program itself, but at least I can control the timeout. The alternative seems to be an endless loop of asynchronous network.request() calls every n seconds pinging for network connectivity. 

How have you guys tackled this problem? 

The use of sockets to ping google is a perfectly valid test.

Thanks Rob. I’ve tracked down the culprit to the statement that loads the Game Analytics plugin. I’ve wrapped it in a timer, as follows:

print(os.time()) local GameAnalytics = require ( "plugin.gameanalytics" ) print(os.time())

And what gets printed to the console is astonishing. Importing this plugin without a network connection blocks the script for  40 seconds

07-25 12:48:58.032: I/Corona(11420): 1437842938 07-25 12:49:38.002: I/Corona(11420): 1437842978

Of course I then tested it with a network connection, and the timers returned the same value (i.e. the module loaded in <1 second). The only difference between the two tests was the device (Moto X) being in airplane mode, or having a WiFi connection.

Either I’m doing something wrong (always a strong possibility) or I’ll need to wrap all my GA code in a network availability check, which would really suck because GA is supposed to work really well offline, including archiving events for when the connection is reestablished. 

I’ve emailed support@gameanalytics.com as suggested in the documentation. We’ll see what they say.

It’s possible they could be doing network activity in the main chunk of the module instead of waiting for an init call, so wrapping it in a network test may be a wise option.

Here’s a nifty script someone published for checking connectivity. It tries to use the built in network.setStatusListener method if available, and if not checks using sockets. 

https://gist.github.com/ProGM/9786014

The plot thickens…this 40 second block seems to happen anywhere sockets are used, regardless of what the timeout value is set to.

And thickens further…I can reproduce this in countless ways on my Moto X testing device, but on my Nexus 4 and Nexus 7, the timeout appears to be respected, and the app loads in a second or two. 

I’ve tried clearing the app data/cache, but no change. 

The use of sockets to ping google is a perfectly valid test.

Thanks Rob. I’ve tracked down the culprit to the statement that loads the Game Analytics plugin. I’ve wrapped it in a timer, as follows:

print(os.time()) local GameAnalytics = require ( "plugin.gameanalytics" ) print(os.time())

And what gets printed to the console is astonishing. Importing this plugin without a network connection blocks the script for  40 seconds

07-25 12:48:58.032: I/Corona(11420): 1437842938 07-25 12:49:38.002: I/Corona(11420): 1437842978

Of course I then tested it with a network connection, and the timers returned the same value (i.e. the module loaded in <1 second). The only difference between the two tests was the device (Moto X) being in airplane mode, or having a WiFi connection.

Either I’m doing something wrong (always a strong possibility) or I’ll need to wrap all my GA code in a network availability check, which would really suck because GA is supposed to work really well offline, including archiving events for when the connection is reestablished. 

I’ve emailed support@gameanalytics.com as suggested in the documentation. We’ll see what they say.

It’s possible they could be doing network activity in the main chunk of the module instead of waiting for an init call, so wrapping it in a network test may be a wise option.

Here’s a nifty script someone published for checking connectivity. It tries to use the built in network.setStatusListener method if available, and if not checks using sockets. 

https://gist.github.com/ProGM/9786014

The plot thickens…this 40 second block seems to happen anywhere sockets are used, regardless of what the timeout value is set to.

And thickens further…I can reproduce this in countless ways on my Moto X testing device, but on my Nexus 4 and Nexus 7, the timeout appears to be respected, and the app loads in a second or two. 

I’ve tried clearing the app data/cache, but no change.