Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am a very novice android developer but also created a game using andEngine successfully. Now I want to integrate Scoreloop and everything is fine with Leaderboard view and TOS view, except I am having hard time submitting scores from a game scene. I implemented OnScoreSubmitObserver in the main BaseGameActivity (GameActivity). I used the following line inside the onCreateEngineOptions method:

ScoreloopManagerSingleton.get().setOnScoreSubmitObserver(this);

I also declared the override method perfectly inside GameActivity as mentioned in the scoreloop tutorial:

@Override
public void onScoreSubmit(int status, Exception error) {
    // TODO Auto-generated method stub
    //Calls the ShowResultOverlayActivity. Make sure you have modified the
    //AndroidManifest.xml to reference this overlay class.
    startActivity(new Intent(this, ShowResultOverlayActivity.class));
}

Now I want to submit score from inside a scene using the following line:

ScoreloopManagerSingleton.get().onGamePlayEnded(score, null);

But this shows the following error:

java.lang.IllegalStateException: you are not calling from the main thread context

I also tried implementing OnScoreSubmitObserver on the scene class and tried calling each line mentioned above inside the scene class using "activity" instead of "this", as "activity" is an instance of the GameActivity. but it also shows the same error. Please help.

share|improve this question
    
So which thread are you calling onGamePlayEnded function from? (also, this isn't specific to game development, you should probably ask on Stackoverflow.com) –  Byte56 Sep 20 '13 at 15:27
    
I totally don't know what is the deal with a thread here. It only works only if I call it directly inside the GameActivity. But I need to call it from a game scene, which is not working. –  Nafis Abdullah Khan Sep 20 '13 at 15:39
add comment

1 Answer

up vote 2 down vote accepted

Create a Runnable object that executes the update to Scoreloop, then run that object from whatever thread you like with runOnUIThread. This will always ensure the code runs on the same thread.

Your runnable would look something like:

public class ScoreLoopUpdater implements Runnable {
  private float score;
  public ScoreLoopUpdater (Float _score) {
    this.score = _score;
  }

  public void run() {
    ScoreloopManagerSingleton.get().onGamePlayEnded(score, null);
  }
}

So using it would look like:

ScoreLoopUpdater scoreLoopUpdater = new ScoreLoopUpdater(finalScore);
Activity.runOnUiThread(scoreLoopUpdater);

If the UI thread doesn't work, you can try the same on the "main" thread:

Handler mainHandler = new Handler(context.getMainLooper());

mainHandler.post(scoreLoopUpdater);
share|improve this answer
    
Didn't work. It shows the exact same error. –  Nafis Abdullah Khan Sep 20 '13 at 16:49
    
I am having a sense that I should use runOnUpdateThread instead of using runOnUiThread. I will let you know after trying. –  Nafis Abdullah Khan Sep 20 '13 at 17:06
    
Nope, sorry, it's the same error!! –  Nafis Abdullah Khan Sep 20 '13 at 17:27
    
Please update the question with the full stack trace. –  Byte56 Sep 20 '13 at 17:28
    
Yes, problem is solved. Your solutions are correct. It's just I used "ScoreloopManagerSingleton.get().onGamePlayEnded(score, null);" twice; one was hiding somewhere else. That one was showing the same error all the time. I beg your pardon for this inconvenience. –  Nafis Abdullah Khan Sep 20 '13 at 17:41
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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