Search Unity

Kongregate API Bug

Discussion in 'Scripting' started by hannes-dev, Jul 21, 2013.

  1. hannes-dev

    hannes-dev

    Joined:
    Apr 27, 2012
    Posts:
    132
    Hello

    for some reason I can't get the username from the Kongregate servers.
    it used to work in another game i made:
    www.kongregate.com/games/hannesdelbeke/words-from-space
    when i try the exact same code in another project it doesn't work

    so i disable everything except the kong API and rebuild "words from space"
    it shows a black screen with the username on it
    now I make a pakage of it, put it in my other project, disable everything, and uplaod it
    and it still doesn't work

    I tried more then 10 times with different builds and adjusting the script etc, but none works
    I don't get why not, since it is the exact same thing

    sending statistics to the server works fine in both projects
    and when I debug it with firebug both log in correctly
    so i have no cluea why it shows up on 1 project and not in the other

    this is my API code
    Code (csharp):
    1. //#pragma strict
    2.  
    3. var isKongregate : boolean = false;
    4. var userId : int = 0;
    5. var username = "Guest";
    6. var gameAuthToken : String = "";
    7. var kongregate ;
    8. var UsernameObj : GameObject ;
    9. public var guiSkin : GUISkin;
    10.  
    11.  
    12. // Begin the API loading process if it is available
    13. Application.ExternalEval
    14. (
    15.   "if(typeof(kongregateUnitySupport) != 'undefined'){" +
    16.   " kongregateUnitySupport.initAPI('KongregateAPI', 'OnKongregateAPILoaded');" +
    17.   "};"
    18. );
    19. // Register a sign in handler to let us know if the user signs in to Kongregate. Notice how we are using the
    20. // Javascript API along with Application.ExternalEval, and then calling back into our app using SendMessage.
    21. // We deliver the new user information as a simple pipe-delimited string, which we can easily parse using String.Split.
    22. Application.ExternalEval(
    23. "kongregate.services.addEventListener('login', function(){" +
    24. "   var services = kongregate.services;" +
    25. "   var params=[services.getUserId(), services.getUsername(), services.getGameAuthToken()].join('|');" +
    26. "   kongregateUnitySupport.getUnityObject().SendMessage('MyUnityObject', 'OnKongregateUserSignedIn', params);" +
    27. "});"
    28.  );
    29.    
    30. function Awake () {
    31. DontDestroyOnLoad (transform.gameObject);
    32. username = "Guest";
    33. }
    34.  
    35. /*
    36. function Start () {
    37.  
    38. }
    39. */
    40.  
    41.  
    42. function OnGUI()
    43. {
    44.              GUI.skin = guiSkin;
    45. if(username)
    46.  GUI.Label (new Rect (10,130,300,40), ""+ username);
    47. if(  isKongregate)
    48.  GUI.Label (new Rect (10,170,150,40), "connection established");
    49.  
    50. }
    51. function Update () {
    52.     /*if(username == "hannesdelbeke"){
    53.         Camera.main.GetComponent(CubeControl).createLevel = true;
    54.     }*/
    55.         if(UsernameObj)
    56.         UsernameObj.GetComponent(TextMesh).text = username ;
    57. }
    58. /*
    59. function LoadAPI ()
    60. {
    61.  
    62. }*/
    63.  
    64. function OnKongregateAPILoaded(userInfoString){
    65.   // We now know we're on Kongregate
    66.   print("UNITY function loaded");
    67.    Debug.Log("ON KONG");
    68.   isKongregate = true;
    69.  
    70.   // Split the user info up into tokens
    71.   var params = userInfoString.Split("|"[0]);
    72.   userId = parseInt(params[0]);
    73.   username = params[1];
    74.   gameAuthToken = params[2];
    75. }
    76.     // Called when the Kongregate user signs in, parse the tokenized user-info string that we
    77.     // generate below using Javascript.
    78. function OnKongregateUserSignedIn(userInfoString){
    79.     var params = userInfoString.Split("|"[0]);
    80.     userId = parseInt(params[0]);
    81.     username = params[1];
    82.     gameAuthToken = params[2];
    83.      
    84. }
    85.  
    86. function SubmitScoreLvl(myScore : int , lvl : int)
    87. {
    88. // Begin the API loading process if it is available
    89. Application.ExternalCall("kongregate.stats.submit","ScoreLvl"+lvl,myScore);
    90.    Debug.Log("score of " +myScore + " submitted");
    91.    
    92. }
    93. function SubmitUnlockLvl(_unlockedLevel : int){
    94.     Application.ExternalCall("kongregate.stats.submit","Unlocked",_unlockedLevel);
    95. }
    96. function SubmitCompleteLvl(_completedLevel : int){
    97.     Application.ExternalCall("kongregate.stats.submit","Completed",_completedLevel);
    98. }
    99.  
    100. /*
    101. // Assuming we have a TextField named "my_txt":
    102. function scoresCallback ( result:Object ):void
    103. {
    104.     my_txt.appendText("High score result, success=" + result.success );
    105.     for ( var i:int = 0; i < result.list.length; i++ ){
    106.         var position:int = i + 1;
    107.         my_txt.appendText("\n"+position + ". " + result.list[i].username + " - " + result.list[i].score );
    108.     }
    109. }
    110. // Request the list
    111. kongregate.scores.requestList ( scoresCallback );
    112. */
     
  2. hannes-dev

    hannes-dev

    Joined:
    Apr 27, 2012
    Posts:
    132
    Ok I found it
    but I don't understand why it works in all my other projects
    I assume something to do with the order Unity runs his scripts maybe

    so i placed the 2 Application.ExternalEval in the awake and that solved it
     
  3. median

    median

    Joined:
    May 9, 2012
    Posts:
    5
    Hi Dhajku
    I have a silly question : could you tell me did you complete converting the "score Callback" function to unity? I have a huge problem on it.