Game Center init freezes application momentarily

Hi Corona Community

Whilst I believe this is a common issue that cannot be solved I thought I would open it up for discussion. After my game loads for the first time I automatically call game network.init() for Game Center, however like a lot of other games some of which are quite well know this freezes my game for a split second. 

There are a few ways in which I could handle this like pausing the game during autentication but ideally would like it if Game Center just didn’t do this at all  :stuck_out_tongue:

Does anyone know if this lag which occurs during the manual init or sometimes during the subsequent suspend/resume automatic init calls can be fixed ? Or should I just grin and bear it?

Cheers

Simon

Lua supports something called Coroutines. These are kind of like threads that run separately from the main thread.  See:
 

https://coronalabs.com/blog/2015/02/10/tutorial-using-coroutines-in-corona/

or you could try putting it in a timer.performWithDelay()

Rob

Cheers again Rob, I tried setting timers and also put it in a Coroutines without any luckI have decided to just handle the init call and subsequent auto init calls via a loading overlay which pops up after the Runtime suspend/resume, and during the initial load of the game. This although not ideal stops the lag from affecting the gameplay, I reckon that it has something to do with Game Center making network/ping requests as when wifi and mobile data is off the laggyness isn’t as noticeable. There is a heap of forum posts regarding how bad Game Center is for iOS9 devices so maybe this might get resolved in a future update of the OS. Anyway problem solved onto the next :stuck_out_tongue:

Whilst implementing the overlay/GC interrupt screen I am using the system events “suspend” and "resume"I have noticed some interesting behaviour.

The automatic init call on resume from Game Center is not triggered if the suspend was initiated by either the top slide tray (notifications) or the bottom one (wifi brightness etc)  I have only tested this on my 5c iOS 9 so far… Have you had any experience with these system events Rob or any other gurus?

Is their a way to identify the events trigger source as I only want to know when the genuine Game Center.init automatic function call is being triggered?

I am going to delve deeper via Google but thought I would see if anyone had any ideas.

Cheers Simon

Edit:

Just for clarity here is the gotcha from the docs

Be aware that iOS backgrounding will cause your app to automatically log out the user from Game Center. When the app is resumed, Game Center will automatically try to re-login the user. The callback function you specified here will be invoked again, providing the result of that re-login attempt. As such, this callback function exists for the life of your application. Note that other Game Center functions should not be called when the user is not logged in

I notice it says ‘backgrounding’ not suspended. Can this ‘backgrounding’ event be tracked? 

You get a suspend event when your app is backgrounded for any reason. I don’t know if the notifications pull down or quick settings pull up actually suspend your app or not.

Rob

As I mentioned my test device triggers the event.type “applicationSuspend”/ “applicationResume” when I use the pull down/up menus on my 5c iOS9. This behaviour is fine however when this occurs the auto game centre init doesn’t get called on resume from the slide tray action (GC doesn’t get logged out by the OS when the slide trays are swiped)

So I need a way to differentiate between when my app is suspended due to the slide trays and when it is a genuine suspend (home bu, lock bu, phone call etc) . Will have to test some other devices/iOS version today to see if this behaviour is consistent. 

Cheers

Simon

Why not just init yourself on resume?

As Game Center is still logged in all I get is this message, I don’t think I can capture specific WARNING messages…

WARNING: gameNetwork.init() was already called for gamecenter.

The work around here is to just set a timeout function after a few secs but then I might not capture the  ALL then GC auto inits which is the point of this exercise.

It’s a warning. You can ignore it.

Rob

Ok I have found some native code that would help:

Here is a way of natively checking to see if the user is logged into Game Center or not. If I could access this function I could then not bother them with the loading overlay and just let them play straight away after resume.

if([GKLocalPlayer localPlayer].isAuthenticated) { // user logged in }

Reference

http://stackoverflow.com/questions/31218209/check-if-a-game-center-account-is-logged-in

Feature request perhaps.

I’m currently writing a full featured Game Center plugin for Corona SDK that will give you more control of when the Game Center Sign In screen is shown to the player, so no need to post a feature request. I can’t say exactly when it will be ready but it’s in the final stages before I open it up for beta testing which I hope will be near the end of October.

The Corona SDK gameNetwork iOS Game Center plugin does dispatch a lua event for [GKLocalPlayer localPlayer].isAuthenticated

On lines 1017 and 1031 of the plugin: https://github.com/coronalabs/plugins-source-gamenetwork-apple/blob/master/ios/Plugin/Rtt_IPhoneGameCenter.mm

[GKLocalPlayer localPlayer].isAuthenticated YES/NO == event.data true/false

[lua]

loggedIntoGC = false – global or property on composer.loggedIntoGC

local function initCallback( event )

    if ( event.type == “showSignIn” ) then

        – This is an opportunity to pause your game or do other things you might need to do while the Game Center Sign-In screen is up.

    elseif ( event.type == “init” ) then

        if ( event.data == true ) then

            loggedIntoGC = true

        else

            loggedIntoGC = false

    end

end

[/lua]

I should add that the initCallback() gets called again after your game is resumed from being suspended so you don’t have to listen for the “applicationSuspend” and “applicationResume” “system” events and you should only have to call gameNetwork.init( “gamecenter”, initCallback ) once when your game is launched.

Thanks animonger, I will defiantly be keen on beta testing sounds like a good solution.

Cheers 

Simon

Great, I’ll post a link here when it’s ready.

Lua supports something called Coroutines. These are kind of like threads that run separately from the main thread.  See:
 

https://coronalabs.com/blog/2015/02/10/tutorial-using-coroutines-in-corona/

or you could try putting it in a timer.performWithDelay()

Rob

Cheers again Rob, I tried setting timers and also put it in a Coroutines without any luckI have decided to just handle the init call and subsequent auto init calls via a loading overlay which pops up after the Runtime suspend/resume, and during the initial load of the game. This although not ideal stops the lag from affecting the gameplay, I reckon that it has something to do with Game Center making network/ping requests as when wifi and mobile data is off the laggyness isn’t as noticeable. There is a heap of forum posts regarding how bad Game Center is for iOS9 devices so maybe this might get resolved in a future update of the OS. Anyway problem solved onto the next :stuck_out_tongue:

Whilst implementing the overlay/GC interrupt screen I am using the system events “suspend” and "resume"I have noticed some interesting behaviour.

The automatic init call on resume from Game Center is not triggered if the suspend was initiated by either the top slide tray (notifications) or the bottom one (wifi brightness etc)  I have only tested this on my 5c iOS 9 so far… Have you had any experience with these system events Rob or any other gurus?

Is their a way to identify the events trigger source as I only want to know when the genuine Game Center.init automatic function call is being triggered?

I am going to delve deeper via Google but thought I would see if anyone had any ideas.

Cheers Simon

Edit:

Just for clarity here is the gotcha from the docs

Be aware that iOS backgrounding will cause your app to automatically log out the user from Game Center. When the app is resumed, Game Center will automatically try to re-login the user. The callback function you specified here will be invoked again, providing the result of that re-login attempt. As such, this callback function exists for the life of your application. Note that other Game Center functions should not be called when the user is not logged in

I notice it says ‘backgrounding’ not suspended. Can this ‘backgrounding’ event be tracked? 

You get a suspend event when your app is backgrounded for any reason. I don’t know if the notifications pull down or quick settings pull up actually suspend your app or not.

Rob

As I mentioned my test device triggers the event.type “applicationSuspend”/ “applicationResume” when I use the pull down/up menus on my 5c iOS9. This behaviour is fine however when this occurs the auto game centre init doesn’t get called on resume from the slide tray action (GC doesn’t get logged out by the OS when the slide trays are swiped)

So I need a way to differentiate between when my app is suspended due to the slide trays and when it is a genuine suspend (home bu, lock bu, phone call etc) . Will have to test some other devices/iOS version today to see if this behaviour is consistent. 

Cheers

Simon