Search code examples
androidmultiplayerillegalstateexceptiongkturnbasedmatch

IllegalStateException ParticipantResult TurnbasedMultiplayer


I am very novice so please excuse my bad coding habits, but I encountered an IllegalStateException Error but I have no clue why.

I am making a game where I want a match to be 1 turn each player. So, if you play with me and I started the match, then I will first play my turn and then send you the needed info, whereafter you play your turn and send your data back with the results of the match.

However I am having problem with finishing my turn ("my" being the one who started the match). I want to send the info to the other player, however (from what I gathered) I will have to finish the match and send the needed data as well. I do this with the following piece of code:

String playerId = Games.Players.getCurrentPlayerId(mGoogleApiClient);
                        String myParticipantId = mMatch.getParticipantId(playerId);
                        ParticipantResult myresult = new ParticipantResult(myParticipantId,ParticipantResult.MATCH_RESULT_UNINITIALIZED, ParticipantResult.PLACING_UNINITIALIZED);
                        ParticipantResult opponentresult = new ParticipantResult(getNextParticipantId(),ParticipantResult.MATCH_RESULT_UNINITIALIZED, ParticipantResult.PLACING_UNINITIALIZED);
                        List<ParticipantResult> reportresult = new ArrayList<ParticipantResult>();
                        reportresult.add(myresult);
                        reportresult.add(opponentresult);
                        Games.TurnBasedMultiplayer.finishMatch(mGoogleApiClient, mMatch.getMatchId(),mTurnData.persist(), reportresult)
                        .setResultCallback(new ResultCallback<TurnBasedMultiplayer.UpdateMatchResult>() {
                            @Override
                            public void onResult(TurnBasedMultiplayer.UpdateMatchResult result) {
                                processResult(result);
                            }
                        });

The error I get is mentioned at the bottom of my post. The line 2717 is the this line:

ParticipantResult myresult = new ParticipantResult(myParticipantId,ParticipantResult.MATCH_RESULT_UNINITIALIZED, ParticipantResult.PLACING_UNINITIALIZED);

So what am I doing wrong here? To my knowledge this should work... Honestly, I would like to avoid sending participant results, but since I need to send data, I need to mention results according to: com.google.android.gms.games.multiplayer.turnbased.TurnBasedMultiplayer.finishMatch(GoogleApiClient arg0, String arg1, byte[] arg2, List<ParticipantResult> arg3)

12-25 04:41:36.995: E/AndroidRuntime(19132): FATAL EXCEPTION: main
12-25 04:41:36.995: E/AndroidRuntime(19132): Process: com.devsid.quiz, PID: 19132
12-25 04:41:36.995: E/AndroidRuntime(19132): java.lang.IllegalStateException
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.google.android.gms.internal.jx.K(Unknown Source)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.google.android.gms.games.multiplayer.ParticipantResult.<init>(Unknown Source)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.google.android.gms.games.multiplayer.ParticipantResult.<init>(Unknown Source)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.devsid.quiz.QuizActivity$52.onClick(QuizActivity.java:2717)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.view.View.performClick(View.java:4438)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.view.View$PerformClick.run(View.java:18439)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.os.Handler.handleCallback(Handler.java:733)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.os.Looper.loop(Looper.java:136)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.app.ActivityThread.main(ActivityThread.java:5158)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at java.lang.reflect.Method.invokeNative(Native Method)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at java.lang.reflect.Method.invoke(Method.java:515)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at dalvik.system.NativeStart.main(Native Method)    

Solution

  • I found that you cannot really hold match data in a static and still call it using the google functions, maybe it is coded this way to stop too much traffic.

    You can store the matchID into a variable and use it to make sure that you are updating for the correct match, but you cannot call google methods of match, or you will get NULL results.

    From my experience, the only time you can call the google match methods is inside your processResult() function. Like this...

    public void processResult(UpdateMatchResult result) {
        TurnBasedMatch updateMatch = result.getMatch();
    

    Otherwise the data is stale.