- Home /
How do I implement the Kongregate api
I followed the official kongregate unity api integration docs, but I can't get the api to initialize.
(Take a look at the docs here: http://www.kongregate.com/developer_center/docs/using-the-api-with-unity3d)
Here is the code from kongregate that is supposed to start the initialization process:
// Begin the API loading process if it is available
Application.ExternalEval(
"if(typeof(kongregateUnitySupport) != 'undefined'){" +
" kongregateUnitySupport.initAPI('MyUnityObject', 'OnKongregateAPILoaded');" +
"}";
);
I don't find the official docs to be clear on what to do exactly. I do understand how the submit statistic feature works though.
Thanks for your help.
Sincerely, MeatbagWammo
$$anonymous$$aybe we need a bounty on this question...
If you add bounty to this I'll give you 50 rep (I'll post a question, add bounty, you answer it and get the bounty). Unfortunately I cannot add bounty to someone elses question and posting another question would be stupid.
Answer by TastyGraph · Jan 03, 2011 at 06:31 AM
I did the following with my game on Kongregate, and the process works fine for me in both Unpublished/Testmode as well as Published mode (but only on Kongregate, not while testing in the editor)
However, do keep in mind that I experienced delays of up to 30-60 seconds from the time my game submitted the stat, to the time it actually popped up on the highscores tab on Kongregate. So dont get impatient while testing... cough like I did :P
I created an EMPTY GAMEOBJECT, renamed it "KongregateAPI" and attached the following UnityScript/Javascript file onto it. (I also called the script KongregateAPI.js , I dont know if this is relevant or not)
KongregateAPI.js
////////////////////////////////////////////////////////////////////////////// // // // SUBMIT STATISTICS WITH // // // // Application.ExternalCall("kongregate.stats.submit","MatchesMade",1); // // // //////////////////////////////////////////////////////////////////////////////
var isKongregate = false; var userId = 0; static var username = "Guest"; var gameAuthToken = "";
function OnKongregateAPILoaded(userInfoString){ // We now know we're on Kongregate isKongregate = true; Debug.Log("ON KONG");
// Split the user info up into tokens var params = userInfoString.Split("|"[0]); userId = parseInt(params[0]); username = params[1]; gameAuthToken = params[2]; }
function Awake() { // This game object needs to survive multiple levels DontDestroyOnLoad (this);
// Begin the API loading process if it is available
Application.ExternalEval( "if(typeof(kongregateUnitySupport) != 'undefined'){" + " kongregateUnitySupport.initAPI('KongregateAPI', 'OnKongregateAPILoaded');" + "}" ); }
This KongregateAPI object I then placed in my opening/splash scene to ensure that its loaded first/early and only loaded once, since the DontDestroyOnLoad(this); line in the Awake() function ensures that the object doesnt die on scene switch.
I would then add in the following line in order to make the game submit statistics to Kongregate. For instance, I would place it after finishing a level, to keep track of how many points the player scored on each individual level. As well as the Final Highscore, and also keep track of the highest number of combos the player achieved.
Application.ExternalCall("kongregate.stats.submit","NameOfStatistic",integerValue);
Its important to note that the NameOfStatistic has to match a statistic you've already created in the EDIT Section of your Game on Kongregate after OR during your upload.
So I would replace NameOfStatistic with MaxCombo if I had created a statistic called MaxCombo for my game on Kongregate.
The integerValue variable has to be an integer, even if you're submitting something that is essentially a boolean (then you would submit 1 for true, 0 for false)
There's more info on this at: http://www.kongregate.com/developer_center/docs/statistics-api-tips
I havent tried any of the more (in my humble newbie opinion) advanced stuff like getting the username and using it ingame... I still find it very confusing to even figure out which of the API features are ActionScript only, and which can be used for Unity3d as well.
I tried it, it doesn't work. $$anonymous$$y game doesn't register that it's connected.
This was posted on $$anonymous$$ongregate's forum and I've been messaged by other people thanking me that it worked for them. Its the method I am using, and its working for me. There has to be some other factor that causing your game to not work with this method.
How the hell does this only have one upvote? :s well extra one from me, thanks for the great answer. Wtg.
Answer by YoshieMaster · Dec 30, 2010 at 02:02 AM
AT THE MOMENT THE KONG API DOESN'T SEEM TO WORK. YOU MAY AS WELL TRY THE BELOW, BUT IT PROBABLY WONT WORK.
From what I understand, you need to create an object for Kongregate callbacks. I created an object called KongregateObject. Then in this object you place a script. The first thing I did in my script was set DontDestroyOnLoad(transform.gameObject); This means that your object will survive through different scenes. Then you only have to put the object in the first scene of your game.
These are the variable declarations I made:
var isKongregate = false; var userId = 0;
var username = "Guest"; var gameAuthToken = "";
To initialise the Kong API I used the following line of code:
Application.ExternalEval("if(typeof(kongregateUnitySupport) != 'undefined'){kongregateUnitySupport.initAPI('KongregateObject', 'OnKongregateAPILoaded');}");
To use this code you need a callback function. What this does is when the Kong API is successfully loaded it gives you information such as the unique User ID, the UserName and the Game Authentication Token.
This is the example callback function. Put it in the same script.
function OnKongregateAPILoaded(userInfoString){ // We now know we're on Kongregate isKongregate = true;
// Split the user info up into tokens var params = userInfoString.Split("|"[0]); userId = parseInt(params[0]); username = params[1]; gameAuthToken = params[2]; }
If you want to know if someone has signed in and what their username is, put this code just below the initialisation.
Application.ExternalEval("kongregate.services.addEventListener('login', function(){var services = kongregate.services;var params=[services.getUserId(), services.getUsername(), services.getGameAuthToken()].join('|');kongregateUnitySupport.getUnityObject().SendMessage('KongregateObject', 'OnKongregateUserSignedIn', params);");
And of course your callback function:
// Called when the Kongregate user signs in, parse the tokenized user-info string that we
// generate below using Javascript.
function OnKongregateUserSignedIn(userInfoString){
var params = userInfoString.Split("|"[0]);
userId = parseInt(params[0]);
username = params[1];
gameAuthToken = params[2];
}
So here is the whole script:
var isKongregate = false; var userId = 0;
var username = "NothingMuch";
var gameAuthToken = "";
// Begin the API loading process if it is available
Application.ExternalEval("if(typeof(kongregateUnitySupport) != 'undefined'){kongregateUnitySupport.initAPI('KongregateObject', 'OnKongregateAPILoaded');}");
// Register a sign in handler to let us know if the user signs in to Kongregate. Notice how we are using the
// Javascript API along with Application.ExternalEval, and then calling back into our app using SendMessage.
// We deliver the new user information as a simple pipe-delimited string, which we can easily parse using String.Split.
Application.ExternalEval("kongregate.services.addEventListener('login', function(){var services = kongregate.services;var params=[services.getUserId(), services.getUsername(), services.getGameAuthToken()].join('|');kongregateUnitySupport.getUnityObject().SendMessage('KongregateObject', 'OnKongregateUserSignedIn', params);");
function Awake () {
DontDestroyOnLoad (transform.gameObject);
username = "Guest";
}
function OnKongregateAPILoaded(userInfoString){
// We now know we're on Kongregate
isKongregate = true;
// Split the user info up into tokens
var params = userInfoString.Split("|"[0]);
userId = parseInt(params[0]);
username = params[1];
gameAuthToken = params[2];
}
// Called when the Kongregate user signs in, parse the tokenized user-info string that we
// generate below using Javascript.
function OnKongregateUserSignedIn(userInfoString){
var params = userInfoString.Split("|"[0]);
userId = parseInt(params[0]);
username = params[1];
gameAuth
Token = params[2]; }
Good Luck! Hope this works better for you than it did for me. If you get it to work, please tell me what you did.
Answer by _Petroz · Dec 30, 2010 at 11:46 PM
Find out how to extend your Unity game to communicate with the Kongregate back-end with our Kongregate bindings.
Taken from: http://unity3d.com/kongregate/
Doesn't work, I added the two scripts to objects and uploaded and it says: Connected?: False Username: Guest UserId: 0 isGuest: True
Probably an issue at $$anonymous$$ongregates end. The deadline for the comp ticks nearer...
Sorry for triple post but I just had to say, wouldn't it be funny if the deadline came and noone was eligible because they couldn't implement the API?
Answer by mcunha98 · Jan 12, 2013 at 11:56 PM
If you receive an error about split method dont recognize as a member of Object, change the parameter of function.
function OnKongregateAPILoaded(userInfoString : String)
function OnKongregateUserSignedIn(userInfoString : String)
Thank you for this tip. I ended up removing #param strict from the top and it got rid of this error.
Your answer
Follow this Question
Related Questions
Help with Kongregate API? 0 Answers
Cannot initialise Kong API 2 Answers
Using the Kongregate API 1 Answer
Kongregate Shared Content API 1 Answer
Javascript in Unity, Including iPhone API's (Objective C?) 2 Answers