Wednesday, December 31, 2014

Unity3D Kongregate API step by step javascript

Okay, I manage to get the highscore appear in kongregate , you can see it yourself at

http://www.kongregate.com/games/fundurian/idle-zombie-spitter


Okay, because the unity is very confusing with tons of object and tons of script, I put all the below code inside my main engine java script

lets start

Step1: Open your main engine java script (my case is shoot.js)

Step2: create 4 variable
var KONGREGATE_LOADEDboolean = false;
var USER_IDint =0;
var USER_NAMEString="Guest";
var GAME_AUTH_TOKENString = "";


Step3: under the function start(), call the kongregate webserver for the kongregate unity support

function Start()
{

Application.ExternalEval("if(typeof(kongregateUnitySupport) != 'undefined'){" +" kongregateUnitySupport.initAPI('" + gameObject.name + "','OnKongregateAPILoaded');" + "};");
    

}

Step4: create this 3 function


function OnKongregateAPILoadeduserInfo:String)
{

    KONGREGATE_LOADEDtrue;


    var userStats  = userInfo.Split("|"[0]);
    
    
    
    USER_ID = int.Parse(userStats[0]);
    USER_NAME = userStats[1];
    GAME_AUTH_TOKEN = userStats[2];

}


function APILoaded() : boolean
{

    if((USER_NAME != "Guest") && (KONGREGATE_LOADED==true))
    {
        return true;
    }
    else
    {
        return false;
    }

}

function SubmitData(datanameString , datavalueint)
{
    if(APILoaded()==true)
    {
        Application.ExternalCall("kongregate.stats.submit",dataname,datavalue);
        
        

    }
    

}


Step5: submit the score, you can put it anywhere, example when you win the game or lose the game,
do note konscore is a variable you can use anyname, when your user score 1000 example you just store it into konscore and call this function to submit to kongregate


SubmitData("HighScore",konscore);


Step6: Open kongregate, under my games -> edit games -> upload new version
- Under statistic API -> click add statistic
- statistic name : HighScore  (SUPER SUPER IMPORTANT, MUST MATCH the dataname on top)
- choose max type
- display on leaderboard (tick)

Step7: Now try to play your games, and submit score, wait maybe 5 or 10 minute for it to appear, please be patient


there you go, enjoy, any question feel free to ask

No comments:

Post a Comment