question

thejamiryu avatar image
thejamiryu asked

Most efficient way to save data

I have two questions that I hope you guys help me out.

First, I have more than 50 key/value pairs to save for each player. Is JSON best way for it ? Secondly, I have set one key/value pair and User Data key is successfully stored. My problem is how can I convert "result.Data" to int or float.

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

            { "damage", karakter.damage.ToString() },
           
        }
    }

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);
                karakter.damage = result.Data["damage"].Value;  ====> HERE
            }
    }

Player Data
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

·
JayZuo avatar image
JayZuo answered

Player data is stored as key/value pairs (KVPs) by PlayFab. The value can be any string. JSON is usually a good choice if you want to store some complex data. And Game Manager has built-in JSON support.

To convert "result.Data" to int or float, you can use Convert.ToInt32 or Convert.ToSingle method. For example,

karakter.damage = Convert.ToInt32(result.Data["damage"].Value);

int.Parse or float.Parse can also work.

6 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.

thejamiryu avatar image thejamiryu commented ·

One last question, there are a lot of comments about "aggregate fewer, larger keys" on PlayFab forum. How can I do this and are there any sample or documentation about it ? Thank you.

1 Like 1 ·
JayZuo avatar image JayZuo ♦ thejamiryu commented ·

I‘d think that's say you can put some key/value pairs into one JSON and store the JSON as the value of one key. For example, you have four KVPs like the following:

{
  "Data": {
    "Class": "Fighter",
    "Gender": "Female",
    "Icon": "Guard 3",
    "Theme": "Colorful"
  },
  "Permission": "Public"
}

And you can aggregate them into something like:

{
    "Data": {
        "Unicorn": "{\"Class\":\"Fighter\",\"Gender\":\"Female\",\"Icon\":\"Guard 3\",\"Theme\":\"Colorful\"}"
    },
    "Permission": "Public"
}
1 Like 1 ·
thejamiryu avatar image thejamiryu thejamiryu commented ·

First of all thank you for your guidance. As I understand, I need to write Json file and upload it to Title Data from Game Manager.

{
  "Data": {
    "Skills": [
        { "name":"Bow", "Types":[ "Skill1", "Skill2", "Skill3" ] },
        { "name":"Survivor", "Types":[ "Skill1", "Skill2", "Skill3" ] },
        { "name":"Agility", "Types":[ "Skill1", "Skill2", "Skill3" ] }
    ]
 },
  "Permission": "Public"
}

After this, how can I set and get "skill1" with using SetUserData and GetUserData. Again, thank you very much.

0 Likes 0 ·
thejamiryu avatar image thejamiryu thejamiryu commented ·

After some research on forum, I realized that Title Data won't serve my needs. Because I want these values to keep updated.

0 Likes 0 ·
Show more comments

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.