question

lolaki avatar image
lolaki asked

KongregateHandler and GetUserData

Hi folks. I am trying to get Player Data from my saved data. SetUserData working perfectly fine. I can see Player Data in my Game Manager. However when I paste GetUserData codes, PlayFabId = PlayFabId, give me an error on code. As in the KongregateHandler script, I change it to KongregateId = kongregateId, still give me an error on code. So how I get my saved player data?

These are KongregateHandler and GetUserData scripts.

public void GetUserData()
    {
        PlayFabClientAPI.GetUserData(new GetUserDataRequest()
        {
            PlayFabId = PlayFabId,
            Keys = null
        }, result => {
            Debug.Log("Got user data:");
            if (result.Data == null || !result.Data.ContainsKey("damage")) Debug.Log("No damage");
            else Debug.Log("damage: " + result.Data["damage"].Value);
        }, (error) => {
            Debug.Log("Got error retrieving user data:");
            Debug.Log(error.GenerateErrorReport());
        });
    }
// We are specifically interested in importing PlayFab related namespaces 
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
using UnityEngine.UI;




public class KongregateHandler : MonoBehaviour
{


    // Standard unity callback, executed once for the script to start
    public void Start()
    {
        // Utility: show feedback
        SetMessage("Loading kongregate api...");


        /* 
         * Important: execute Javascript in the external context to initialize
         * Kongregate API, Unity Support and set up callback GameObject and Method.
         * In this case, callback is set to a GameObject called Kongregate and a 
         * method called OnKongregateAPILoaded, which we define later in this class.
         * Once Kongregate API is initialized, Unity will locate this object by name 
         * ("Kongregate") and execute a method "OnKongregateAPILoaded" passing in user 
         * info string as an argument. 
         */
        Application.ExternalEval(
          "if(typeof(kongregateUnitySupport) != 'undefined'){" +
          " kongregateUnitySupport.initAPI('Kongregate', 'OnKongregateAPILoaded');" +
          "} else {" +
          " console.error('No unity support!');" +
          "};"
        );
    }


    /*
     * Executed once Kongregate API is ready. This method is invoked by KongregateAPI 
     * and receives a structured text with multiple pieces of data you must parse manually. 
     * The userInfo string parameter has the following structure: 'user_identifier|user_name|auth_token'
     */
    public void OnKongregateAPILoaded(string userInfo)
    {
        SetMessage("Recieved user info! Logging though playfab...");


        // We split userInfo string using '|' character to acquire auth token and Kongregate ID.
        var userInfoArray = userInfo.Split('|');
        var authTicket = userInfoArray[2];
        var kongregateId = userInfoArray[0];


        LogToBrowser("Auth Token: " + authTicket);
        LogToBrowser("Kongregate Id: " + kongregateId);


        /* 
         * We then execute PlayFab API call called LoginWithKongregate.
         * LoginWithKongregate requires KongregateID and AuthTicket. 
         * We also pass CreateAccount flag, to automatically create player account.
         */
        PlayFabClientAPI.LoginWithKongregate(new LoginWithKongregateRequest
        {
            KongregateId = kongregateId,
            AuthTicket = authTicket,
            CreateAccount = true
        }, OnLoggedIn, OnFailed);
    }


    /* 
     * The rest of the code serves as a utility to process results, log debug statements 
     * and display them using Text message label.
     */


    private void OnLoggedIn(LoginResult obj)
    {
        SetMessage("Logged in through PlayFab!");
    }
    private void OnFailed(PlayFabError error)
    {
        SetMessage("Failed to login in with PlayFab: " + error.GenerateErrorReport());
    }


    private void SetMessage(string message)
    {
        InfoLabel.text = message;
    }


    private void LogToBrowser(string message)
    {
        Application.ExternalEval(string.Format("console.log('{0}')", message));
    }


    public Text InfoLabel;

}

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

lolaki avatar image lolaki commented ·

in addition to this, Debug.Log("damage: " + result.Data["damage"].Value) working just fine.

Debug.Log("Got user data:"); isnt. Most likely I have an error about PlayFabId.

0 Likes 0 ·
lolaki avatar image lolaki lolaki commented ·

Aight all Debug.Logs working now. Only I'm not getting saved datas when I call function.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ lolaki commented ·

It seems there is no TitleId entry in your LoginWithKongregateRequest. It is a required property in the request. Would you please add it and give another try?

If errors still occurred, please share the error messages with us.

0 Likes 0 ·

1 Answer

·
brendan avatar image
brendan answered

Can you define where the PlayFab ID is coming from in your code example above? Can you put in a breakpoint or logging code and let us know specifically what the PlayFab ID is set to in your above code? Also, what is the complete body of the error you get back?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.