Google Play Game Services multiplayer with Activity switching
Asked Answered
S

3

16

In my Android game I have a turn-based multiplayer. Users wait for opponents in the lobby and whenever exactly 3 are matched, they go to a new game room together, which is another Activity than the lobby.

The docs suggest to let the Activities extend BaseGameActivity. But as I switch the Activity while players are already connected, don'I need to place the connectivity parts in a Service which my Activity then binds to?

Has anyone tried already with Game Services? How to begin if I can't use BaseGameActivity?

Schlicher answered 24/5, 2013 at 0:36 Comment(1)
I'm trying to find the documentation for the turn-based multiplayer, but I couldn't find it anymore! I'm pretty sure it was together with the realtime documentation, but now it seems google removed it! Do you know where to find it?Snake
S
23

So, one of the reasons we wrote all the Google Play game services samples as single-Activity games is because switching between Activities require you to disconnect from the GamesClient and connect a new one from the new Activity.

So using Fragments is probably the easiest way to go about this. It's also pretty clean and allows you to make a tablet layout by combining them if you want to.

In particular, if you are setting up a multiplayer game, disconnecting will disconnect you from the room, so you can't switch to a different Activity after starting the handshake :-)

Soninlaw answered 30/5, 2013 at 20:59 Comment(6)
Thank you very much, great to have an authoritative answer :) There are definitely use cases where you have to switch between Activities, e.g. if you have a lobby where players meet and chat, and if enough participants are there, you go to the resource-intensive game Activity. But that should be possible if you put all the Google Play Services stuff into a service which you then bind to from the Activities, right?Schlicher
Exactly. If you use one Activity just to gather around players and pass a list of Player IDs to the second Activity, then the second Activity can create the room with those players. It's all fine as long as there is no Activity switch after multiplayer handshake has started.Soninlaw
@Burno So for example, if i'm in middle of a chess game, while the other player is thinking and before making a move, if i get disconnected then i lose the GamesClient? I mean, is there a way that the move by the other user is notified to me when i come online?Falconiform
Sorry for the rather late comment, but I just googled this. I basically got hired to update an existing game with play services etc. However, I have only done this in the suggested way with fragments, in my own games. However, the company doesnt want me to break up their rather complex structure of activities and fragment. There is e.g. a main activity where all the user data, tracks, login(!) etc is. If a track actually gets selected and launches into a game, a new activity is launched. What's the best practice to maintain the GamesClient connection? Or should we just re-establish the connec?Engross
@Aeefire: If things have not changed during the last few months, you'll have quite some problems to do this. As Bruno Oliveira's comment already says: Activity switches require you to disconnect from the GamesClient, which is not possible after players' handshake.Schlicher
Or...you just don't disconnect from the games client.Bucksaw
L
3

I am in the process of developing a multiplayer game using these new Google Play Game Services. It includes achievements and leaderboards, along with multiplayer.

From the button click sample project, I found that they (Google) used fragments extensively, and stayed within the bounds of a single activity. In my custom game, I jump between activities without a problem.

You'll need to keep a few portions of Google Play Game Services objects around, but a service might be overkill, unless your game requires long-running non-UI code to be executed. From what I've experienced, if you switch between activities, you'll want to keep the id of the Room(s) and participant id(s) that are currently involved in the game.

Since the "connectivity parts" are stateless, just reconnect as needed. You can even pass the room/participant id(s) in to each activity via the Intent bundle (or use the singleton pattern approach). This way, you'll save on battery life, performance, etc.

Lemberg answered 30/5, 2013 at 0:37 Comment(0)
S
1

The documentation explains how to use the Game Services without the BaseGameActivity whenever is needed.

For example, during signin:

https://developers.google.com/games/services/training/signin

Clicking the Sign in button should initiate the sign in flow. If you are using the BaseGameActivity base class provided in the samples, simply call the beginUserInitiatedSignIn() method. Otherwise, you must manually call the connect() method of your GamesClient object.

For your specific question, I don't think it is an issue, all the control is inside Google Play, you just need to get the GamesClient and as far as I understood, the connection made on one activity will be there if you access it from another activity (but I didn't test the multiplayer yet).

Snake answered 24/5, 2013 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.