question

jonaskempeneers193 avatar image
jonaskempeneers193 asked

Can't get data

Hello guys, I'm having a lot of trouble understanding how I get data from the database that PlayFab offers when I'm using Unity.

The problem here is that the Debug.Log() shows the correct data, but it doesn't show in the textview. It seems to be that the method is asynchrous, but I really can't wrap my head around it. I have been stuck on this for some time and have searched and found a lot of info but I just can't seem to implement it. Could some of you guys help me get started?

Thanks in advance

//The following method is for getting the PlayFabId of the current user 

public static string GetPlayFabId()
    {
        PlayFabSettings.TitleId = "22A0";


        var request = new GetAccountInfoRequest();


        PlayFabClientAPI.GetAccountInfo(request, OnGetPlayFabIdSuccess, <br>	OnGetPlayFabIdFailed);


        return request.PlayFabId;
    }


public static string GetMoney(string playFabId)
    {
        string amount = "";


//This one is for getting the data I want, the amount of money that the account has:

        PlayFabClientAPI.GetUserData(new GetUserDataRequest()
        {
            PlayFabId = playFabId,
            Keys = null
        }, result => {
            Debug.Log("Got user data:");


            if (result.Data == null || !result.Data.ContainsKey("Money"))
            {
                Debug.Log("No value for money");
            }
            else 
            {
                amount = result.Data["Money"].Value;
                Debug.Log(amount);
            }
        }, (error) => {
            Debug.Log("Got error retrieving user data:");
            Debug.Log(error.GenerateErrorReport());
        });


        return amount;
    }

//Here is where I call the methods and try to get the data into a textview.

public Text moneyText;
    
	void Start () {
        string playFabId = DataBaseManager.GetPlayFabId();
        Debug.Log(playFabId);
        string money = DataBaseManager.GetMoney(playFabId);
        Debug.Log(money);
        moneyText.text = money;
	}

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

·
Joshua Strunk avatar image
Joshua Strunk answered

Currently, all of your functions are ignoring the fact you must wait for the PlayFab callback to be invoked to get any meaningful data. You should start writing your functions which are needing to return data from PlayFab API call with a void return type to ensure you are not tricking yourself. You will instead need to return the data through an onSuccess callback function. This will end up mirroring the PlayFab API.

The quick version would be

https://gist.github.com/JoshuaStrunk/357f8c8602b2c2de7529ed76ab3dac71

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.

jonaskempeneers193 avatar image jonaskempeneers193 commented ·

Thanks @Joshua Strunk this actually made sense to me finally. Thank you for taking the time to explain it rather than just give the answer!

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.