question

thejamiryu avatar image
thejamiryu asked

GetUserData

I'm newbie about backend and really need help with getuserdata. I have managed to integrate my game with PlayFab's Kongregate Add-on. SetUserData works fine. I can see players saved key/value pairs on my game manager. When I try to copy the GetUserData code sample from documentation, PlayFabId = PlayFabId code line isn't working.

using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;


public class KongregateHandler : MonoBehaviour

{
    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;

public void SetUserData()
    {
        PlayFabClientAPI.UpdateUserData(new UpdateUserDataRequest()
        {
            Data = new Dictionary<string, string>() {


            { "damage", karakter.damage.ToString() },
        }
        },
        result => Debug.Log("Successfully updated user data"),
        error => {
            Debug.Log("Got error setting user data Ancestor to Arthur");
            Debug.Log(error.GenerateErrorReport());
        });
    }
    public void GetUserData()
    {
        PlayFabClientAPI.GetUserData(new GetUserDataRequest()
        {
            PlayFabId = PlayFabId, //THIS LİNE ISN'T WORKİNG//
            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());
        });
    }
}
apis
10 |1200

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

1 Answer

·
Hernando avatar image
Hernando answered

I noticed that you hadn't defined field PlayFabId in the code you posted. The PlayFabId of the user will be returned as a property in LoginResult when the client login by doing LoginWithKongregate. You can get it by modifying method OnLoggedIn and add a line of code to defined field PlayFabId at first.

private string PlayFabId;

...

private void OnLoggedIn(LoginResult obj)    

{        

    PlayFabId = obj.PlayFabId;

}

Beside that, according to API documentation, PlayFabId is "Unique PlayFab identifier of the user to load data for. Optional defaults to yourself if not set. When specified to a PlayFab id of another player, then this will only return public keys for that account."

In short, if you use this API to retrieves data for the current user, you can call the method without parameter PlayFabId.

PlayFabClientAPI.GetUserData(new GetUserDataRequest(){},

			result =>{...},

			(error)=> {...}

);
1 comment
10 |1200

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

thejamiryu avatar image thejamiryu commented ·

Thanks Hernando.

0 Likes 0 ·

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.