Easy GameJolt Flash Api integration for Games (As3)
Gamejolt is an awesome platform for indie developers to showcase their games. And they provide API for implementation of trophies, highscores and user data storage etc. This is a tutorial for implementing the GameJolt Flash API for a flash games & i hope it may help you in implementing it in your next game. And I’m using Flash Develop as the IDE here (although i guess its pretty much same for Adobe Flash as well. If in doubt do a Google search).
Click here to see the Demo in Action
This tutorial covers 4 things
1 User authentication.
2 Score submission
3 Trophy Achievements
4 Setting and getting userData
with the help of an AS3 library by Gamejolt user SumYungGai & towerismtest1 (for his api package with MD5 encryption Download From here). You can checkout the full description and discussion here on this thread.
(I’m not going deep into the http requests, Server calls, Api Calls and the inside working of the AS3 API done by them (which i don’t have much knowledge about). If you find any descriptive mistake here, please notify me through comments.
Step 1 : Download the API
Download the api package with MD5 encryption Download From here. (Which worked for me so far).
In FlashDevelop add the extracted com folder to the src folder.
Step 2 : GameJoltConnect Class
To keep the Gamejolt activities outside of the game stuffs, i created a Singleton class(check this one for reference) and added functions and data related to gamejolt to it.
(Click here to view and save the GameJoltConnect Class)
For Example, to authenticate user, i can call GameJoltConnect.instance.authUser(username,token);
Step 3 : Set Private Key & Game ID
In GameJoltConnect class replace the private key and Game Id with your Game Key & ID (Which can be found on the right side of Achievements tab).
Step 4 : User Authentication
In GameJolt , every user has a username and usertoken (click on show to see your token next to the displayed username on top). So (as far as i know) the username and token are passed to the Flash game when it loads. (How to retrive it [Ref:http://metah.ch/blog/2007/07/passing-variable-from-javascript-to-as3-flash-cs3/]). In your preloader (When the loading is complete) or Main.as file, add this line to retrieve the username and token & pass it to authenticate the data.
var userName:String = String( LoaderInfo(this.root.loaderInfo).parameters.gjapi_username );
var userToken:String = String( LoaderInfo(this.root.loaderInfo).parameters.gjapi_token);
GameJoltConnect.instance.authUser(userName, userToken);
When it calls the auth() function, if the user exists and token is correct, the callback function will be called with either true or false, ie returning success (i’ve set a userVerified boolean to keep track of whether user exists or not).
Step 5 : Adding Highscores
In gamejolt for every game a primary highscore table is created (i believe). So to post a score i just call
GameJoltConnect.instance.addHighScore(scoreValue);
Step 6 : Adding Trophies (Achievements)
Click on the Achievements menu, Click on trophies tab and click on Add Trophy to add a new trophy for the game. Fill in the details, upload a 75×75 pixel awesome pic for it, choose from bronze, silver, Keep it secret etc. that kind of stuff.
After trophy is created it appears with a Trophy ID associated with it. I created 3 trophies for this demo game as shown below.
I added the three trophy id’s in my GameJoltConnect class as
private var firstTrophyId:int = 6100;
private var secondTrophyId:int = 6101;
private var thirdTrophyId:int = 6102;
And when a score hits 20 or 40, i call
GameJoltConnect.instance.addTrophy(0); //0 or 1 or 2 for 3 trophies
where inside it, it actually calls gameJoltApi.addTrophyAchieved(_gameId, _privateKey, userName, userToken, firstTrophyId);
Note : For further simplification you can modify the function to addTrophy(trophyId:int) and call the addTrophyAchieved() with trophyId.
The Trophy may not be visible as achieved at the instant you get a trophy. It may take some time (or after you refresh the page)
Step 7 : Storing and loading userData
Custom data can be retrieved & saved using the getKeyData and setKeyData functions. If you want to store the levels unlocked by a user you can store the level data with
GameJoltConnect.instance.setKeyData(“LEVELS_UNLOCKED”,String(numberOfLevelsUnlocked));
and get the stored data by getKeyData by
GameJoltConnect.instance.getKeyData(“LEVELS_UNLOCKED”);
I have actually implemented the callback function (after reading the data) inside GameJoltConnect. You can implement a callBack function for unlocking levels after reading the saved data inside that function. Feel free to check inside the functions & use it, modify it according to your needs.
Send me your comments, queries and doubts. I will try my best to answer your worries :).
Here is the Full project of the Click it Game (use it at your own risk. Contains less comments)
Good Luck :).
got error :-
C:\Users\Desktop\AS3_API\com\gamejolt\crypt\MD5.as, Line 78 Warning: 1112: Array(x) behaves the same as new Array(x). To cast a value to type Array use the expression x as Array instead of Array(x).
Please solve it…