ScoreLoop integration for the next Android project

For Paper Wars, I already did a aHighscore integration because it is was really easy to handle and fast to implement. But it is rather limited as well because it is a hobby project. Thus a new online-feature backend has been chosen for the next project.
The complete framework is quite extensive – if you take a look to http://www.scoreloop.com/ you’ll find out that there are much more things supported than just highscores. This is quite challenging as well – for a proper integration, you might need to create certain game functionality just to match scoreloop features like friend lists, challenges, highscores and maybe even in-game currency.

For my next project – which is about to go to public beta, soon – I decided to start with high-score integration but move on to support much more functionalities later. Because I am using a OpenGL renderer, I can’t use the CoreUI Package which provides a nice pre-built UI. My architecture is built to support platform-independence. This means:

Platform Application->Game Instance

To make internal communication in the game easier, the Game Instance is a singleton that gets created by the platform dependent application which also creates a update thread and calls a render method of the game. This is quite efficient and flexible – I’m still able to create the whole game mechanics on the Desktop with faster turn-around times and easier debugging and switch to mobile devices once I need.

The problem with scoreloop: it must be called by the main thread of the android application – which is the starting activity. But for my projects, I have three threads running:

  1. The Main Activity
  2. The Update Thread
  3. The OpenGL Thread

Number 2+3 are the actual game instance, which is knowing nothing of thread 1. If things go wrong, funny error messages like this are likely to be thrown to the DDMS:

11-15 10:54:45.493: INFO/dalvikvm(11024): WOW: somebody generated [Landroid/server/data/StackTraceElementData; simultaneously

This happens when an issue is causing my threads 2 and 3 to crash – something like updating and rendering a null pointer. Luckily, the android developers already had a good idea to handle issues like this: the message handler system. This persistent message system if manages state switches by the activities.

The rendering of the scoreloop results happens in the Game interface, but how is it possible to get the data there, if you must access the scoreloop client by the main thread?

  1. Create a new message in the update-thread that sends the request to poll data from the scoreloop client
  2. Fetch the message in the main thread and trigger a ScoresControllerObserver
  3. Display a wait-dialog until a response from the scoreloop server arrives
  4. push the retrieved data into the game instance
  5. update and render the game user interface

Of cause I can’t easily call a method to push the retrieved data into the game – chances are high that 5th step interferes with the game update and render calles (remember: the push into the game happenes by thread 1 while threads 2 and 3 are still working). But this problem can easily be solved by synchronizing the update, render and push-methods like this:

public synchronized void updateScores(LinkedList scoreResults) {};
public synchronized void update() {};
public synchronized void render(){};

Well – if this solution works for things like challenges might be arguable at the moment. But at least it provides a defined interface to get the scoreloop data into the OpenGL game.

This entry was posted in Android and tagged , , . Bookmark the permalink.

One Response to ScoreLoop integration for the next Android project

  1. Lovely sharp submit. By no means believed that it was this effortless. Extolment to you!