1

I am following this guide on google for setting up Google Play Game Services for Android.

At the section for implementing a ResultCallback it says:

In the snippet, MatchInitiatedCallback is a class that implements the ResultCallback interface. You can attach this object to the GoogleApiClient so that your game is notified whenever a match is initiated. To see how the MatchInitiatedCallback is implemented, see Taking the first turn.

public class MatchInitiatedCallback implements
    ResultCallback<TurnBasedMultiplayer.InitiateMatchResult> {

@Override
public void onResult(TurnBasedMultiplayer.InitiateMatchResult result) {
    // Check if the status code is not success.
    Status status = result.getStatus();
    if (status.isSuccess()) {
        showError(status.getStatusCode());
        return;
    }

    TurnBasedMatch match = result.getMatch();

    // If this player is not the first player in this match, continue.
    if (match.getData() != null) {
        showTurnUI(match);
        return;
    }

    // Otherwise, this is the first player. Initialize the game state.
    initGame(match);

    // Let the player take the first turn
    showTurnUI(match);
 }
}

I created a class called "MatchInitiatedCallback" but i don't know what to do with it. I would like to just start a new intent.

    // Let the player take the first turn
    showTurnUI(match);

I tried starting a new intent at the showTurnUI(), but this MatchInitiatedCallback won't let me start a new intent i guess because it doesn't inherit from Activity...(?)

I don't understand these callback things well enough to know where to begin, could anyone point me in the right direction?

clayton33
  • 4,176
  • 10
  • 45
  • 64
  • 1
    I recommend [starting here](http://stackoverflow.com/questions/824234/what-is-a-callback-function). – stkent Jan 02 '15 at 02:16

1 Answers1

2

Take a look at the turn based sample in GitHub: https://github.com/playgameservices/android-basic-samples/blob/master/BasicSamples/SkeletonTbmp/src/main/java/com/google/example/tbmpskeleton

The callback is implemented using an anonymous class that calls a method on the main activity.

    ResultCallback<TurnBasedMultiplayer.InitiateMatchResult> cb = new ResultCallback<TurnBasedMultiplayer.InitiateMatchResult>() {
        @Override
        public void onResult(TurnBasedMultiplayer.InitiateMatchResult result) {
            processResult(result);
        }
    };
    Games.TurnBasedMultiplayer.createMatch(mGoogleApiClient, tbmc).setResultCallback(cb);

The code for processResult can be seen in context of the sample: https://github.com/playgameservices/android-basic-samples/blob/master/BasicSamples/SkeletonTbmp/src/main/java/com/google/example/tbmpskeleton/SkeletonActivity.java#L638

Clayton Wilkinson
  • 4,524
  • 1
  • 16
  • 25