In this post we would be talking about integrating scoreloop in libgdx. We would be assuming that you have created a Scoreloop account and have added a game for which you would have the SECRET String provided by Scoreloop.
It is really easy integrating Scoreloop and we would be using their UI; and so we would just need to open their dashboard and update scores from our code.
Initially you would have to download the sdk , add and compile the ScoreloopUI project in the workspace. Then add the reference in your game project in the Properties->Android->Add option of your android game project.
We would make functions for different tasks required to be done.
void InitScoreLoop(){ ScoreloopManagerSingleton.init(this, SCORELOOP_SECRET); //this refers to activity //SCORELOOP_SECRET is the secret string for the game } void DestroyScoreLoop(){ ScoreloopManagerSingleton.destroy(); }
These are the functions to init and destroy scoreloop. Either you can init scoreloop in create or initialize when the score screen is opened. It is best to have an internal mechanism for storing scores like in a database or a file or in Preferences and if user allows to use scoreloop or any other scoring solution then just update the local values in the score solution.
For our case we store all scores using SharedPreferences and maintain local scoreboard in it and while updating to scoreloop we post score from the saved one. So in case if there is no network connection or the user does not want to use it for the time being, the scores would be maintained locally.
Posting :
In our game we only had one scoreboard so this is a solution for this specific scenario. We will not be handling challenges.
Implement OnScoreSubmitObserver interface in AndroidGame class. This gives a function to implement called onScoreSubmit(int status,Exception error).
First set the class up to be listener for this function.
ScoreloopManagerSingleton.get().setOnScoreSubmitObserver(this);
Then implement the function.
public void onScoreSubmit(int status, Exception error) { // TODO Auto-generated method stub _submitStatus = status; startActivityForResult( new Intent(this, ShowResultOverlayActivity.class), SHOW_RESULT); } void SubmitScore(double score){ ScoreloopManagerSingleton.get().onGamePlayEnded((double) score, null); //score is the score you want to update in scoreloop //second argument is for modes and for a basic scoreboard like the one in the game we did not have any so we pass null }
If you want to post to Social Media like Facebook, Twitter etc, using the feature provided in Scoreloop then the following piece of code has to be executed.
startActivityForResult(new Intent(this,PostScoreOverlayActivity.class), POST_SCORE); //POST_SCORE is our defined constant used to check in OnActivityResult function of Android.
Handling the new intents in OnActivityResult function.
public void OnActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case SHOW_RESULT: //......Some custom changes if needed to be done break; case POST_SCORE: //......Some custom changes if needed to be done break; } }
Finally to open Scoreloop UI to see the scores we need to run this piece of code.
public void OpenScoreloop(){ startActivity(new Intent(this, EntryScreenActivity.class)); }
Now we need to integrate run submit/open functions from our game code. We just need to add OpenScoreLoop,SubmitScore(double score) and InitScoreloop() in our Androidinterface and implement them in desktopGame version also. There we can just leave them empty and in androidGame.java we can implement these functions as shown above.
The different game screens (mainmenu,scoremenu, gameplay) would have a reference to this Game class’ object which implements the interface and we can use this reference to call these functions anywhere in the other screens we made.
Next Tutorial:
In the next tutorial we will discuss about the highscore mode of the game and how to create enemies and collectibles endlessly overtime.
Let us know any of your doubts/suggestions; please put them in the comments section and we would try to answer them.
Thank you for the patience.
Hi, it’s really helpful.Can you pls post source code of it.
Hey great tutorial!
Can you post the source code as would be great to see it as i am new to Java and Libgdx.
Many thanks
Hi, could you please provide complete example app? I’m really stuck on this for hours…Thanks!