2

I'm trying to develop a simple Turn based multiplayer game in android using Play games services. I followed all the steps in the docs: https://developers.google.com/games/services/android/turnbasedMultiplayer#implementing_auto-matching The only difference is that I don't want my players to be able to invite friends I want it to be purely Automatching. The game is only 2 player and it can only start ONCE 2 players have been matched. My problem is it doesn't seem to be automatching players. I run the app on 2 devices and they never seem to find each other... They both connect to the Play Games Services fine but they both just create a new game. Here is my code after I connect the GoogleApiClient.

@Override
public void onConnected(Bundle bundle) {
    pDialog.dismiss();
    Toast.makeText(this, "Connected", Toast.LENGTH_LONG).show();

    showDialog("Matching players");

    Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(1,1,0);
    TurnBasedMatchConfig tbmc=TurnBasedMatchConfig.builder().
setAutoMatchCriteria(autoMatchCriteria).build();

    Games.TurnBasedMultiplayer.createMatch(mGoogleApiClient, 
tbmc).setResultCallback(new MatchInitiatedCallback(this));
}

Here is my MatchInitiatedCallback

public class MatchInitiatedCallback implements    
ResultCallback<TurnBasedMultiplayer.InitiateMatchResult> {

    private Context context;

    public MatchInitiatedCallback(Context c) {
        context = c;
    }

    @Override
    public void onResult(TurnBasedMultiplayer.InitiateMatchResult 
initiateMatchResult) {

        pDialog.dismiss();

        if(!initiateMatchResult.getStatus().isSuccess()) {

            Toast.makeText(context, "ERROR: " + 
initiateMatchResult.getStatus().getStatusCode(), Toast.LENGTH_LONG).show();

            return;
        }

        TurnBasedMatch match = initiateMatchResult.getMatch();

        if(match.getData() != null) {
            Toast.makeText(context, "Player2 " + match.getData().toString(), 
Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(context, "Player1", Toast.LENGTH_LONG).show();
            initGame(match);
        }
    }
}

Both devices show the TOAST that says: "Player1" and call the initGame(match) method which is here:

public void initGame(TurnBasedMatch match) {
    String initialise = "initialised";
    Games.TurnBasedMultiplayer.takeTurn(mGoogleApiClient,   
match.getMatchId(),initialise.getBytes(Charset.forName("UTF-16")),
match.getParticipantId(Games.Players.getCurrentPlayerId(mGoogleApiClient))).
setResultCallback(this);
}

@Override
public void onResult(TurnBasedMultiplayer.UpdateMatchResult 
updateMatchResult) {

    if(updateMatchResult.getMatch().getStatus() == 
TurnBasedMatch.MATCH_STATUS_AUTO_MATCHING) {
        Toast.makeText(this, "Still automatching",    
Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "Not automatching", Toast.LENGTH_LONG).show();
    }

}

And once again they both Display the TOAST: "Still automatching" What am I doing wrong. Why don't the devices automatch. Did I skip a step somewhere. Please help.

1 Answer 1

1

Try to change your initGame method so it specifies null for the next participant (to let Play Game services find a player for auto-matching):

public void initGame(TurnBasedMatch match) {
    String initialise = "initialised";
    Games.TurnBasedMultiplayer.takeTurn(
        mGoogleApiClient,   
        match.getMatchId(),
        initialise.getBytes(Charset.forName("UTF-16")),
        null
        ).setResultCallback(this);
}

Also make sure to use two different google+ accounts on the two devices.

I tried your code and both devices said "Player1" and "Not automatching"(!). After changing to null, first device said "Player1" and "Still automatching" and second device said "Player2 [B@41b08830"

I think your problem is due to the way invitations as well as auto matching is handled by Play Game services. When hooking up participants to a TurnBasedMultiplayer match, invitations are sent to participants one by one. When you call takeTurn in the initGame method, you must specify the next participant to receive an invitation. When that participant has accepted the invitation, he must call takeTurn and specify the next participant to receive an invitation and so on. If you specify null, it means the next invitation goes to an automatched player.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.