question

Kim Strasser avatar image
Kim Strasser asked

How can I get a value from Title Player Account Objects?

I created this object but I don't know how to get the values in the client code.

It's not working with my code code. I get this exception:

"Invalid JSON string\nParameter name: json"
private async Task Getobjectjson()
{
    var result = await PlayFabDataAPI.GetObjectsAsync(new GetObjectsRequest()
    {
        Entity = new PlayFab.DataModels.EntityKey { Id = EntityID, Type = EntityType }
    });

    if (result.Error != null)
        Console.WriteLine("Error.");
    else
    {
        int HealthValue, ManaValue;
        Dictionary<string, object> dic = PlayFabSimpleJson.DeserializeObject<Dictionary<string, object>>(result.Result.Objects.ToString());

        if (dic.TryGetValue("Health", out object a))
            HealthValue = Convert.ToInt32(a);

        if (dic.TryGetValue("Mana", out object b))
            ManaValue = Convert.ToInt32(b);
    }
}

How can I get the values 100(Health) and 10000(Mana) in the client code?

Account Management
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

·
Sarah Zhang avatar image
Sarah Zhang answered

result.Result.Objects is a Dictionary contains several PlayFab.DataModels.ObjectResult so you can’t deserialize it directly. You can refer to the following code.

...
 else
    {
        int HealthValue, ManaValue;
        var objs = result.Objects;
        if (objs.TryGetValue("PlayerData", out PlayFab.DataModels.ObjectResult playerdata))
        {
            Dictionary<string, object> dic = PlayFabSimpleJson.DeserializeObject<Dictionary<string, object>>(playerdata.DataObject.ToString());


            if (dic.TryGetValue("Health", out object a))
                HealthValue = Convert.ToInt32(a);


            if (dic.TryGetValue("Mana", out object b))
                ManaValue = Convert.ToInt32(b);
        }
     
    }
...
2 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.

Nicolas C avatar image Nicolas C commented ·

Why is there "Do not call from client code" in PlayFabSimpleJSON comments ?

0 Likes 0 ·
Nicolas C avatar image Nicolas C Nicolas C commented ·

Here is the comment

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.