question

David Jones avatar image
David Jones asked

Converting UserReadOnlyData in Unity C#


I've been trying to make a data sync feature in my game. I use UserReadOnlyData to keep track of the progress for player challenges. I've noticed that the Userreadonlydata response outputs the data in a different format then title data. I have a function that works for all my title data but I can't seem to get it to work for the user data.

Example for titledata debug call

Debug.Log(result.Data["CharacterLevelRamp"]);
result

{"1":250,"2":550,"3":900,"4":1550,"5":2200,"6":3050,"7":3900,"8":4500,"9":5150,"10":6400,"11":7850,"12":9100,"13":10400,"14":11950,"15":13250,"16":14700,"17":16350,"18":18100,"19":19900,"20":22000,"21":24450,"22":26900,"23":30000,"24":32500,"25":36100}
UnityEngine.Debug:Log(Object)

Example for userreadonlydata debug call

Debug.Log(result.Data["ChallengeProgress"].Value);
result

"{\"TotalSpeed\":0,\"SingleSpeed\":0,\"NoDQ\":0,\"ConsecutiveWins\":0,\"TotalRating\":0,\"TotalWins\":0,\"FriendWins\":0}"
UnityEngine.Debug:Log(Object)

Both of these debugs were called in the success functions for both API calls.

The title data has a method that saves the "CharacterLevelRamp" to a dictionary <string, int>.
I want to do the same exact thing for the player read only data but can't find any documentation on the subject. I'm able to deserialize the second example but it just goes to a string and I'm not able to separate the values.

JSON file for first example -

"CharacterLevelRamp": "{\"1\":250,\"2\":550,\"3\":900,\"4\":1550,\"5\":2200,\"6\":3050,\"7\":3900,\"8\":4500,\"9\":5150,\"10\":6400,\"11\":7850,\"12\":9100,\"13\":10400,\"14\":11950,\"15\":13250,\"16\":14700,\"17\":16350,\"18\":18100,\"19\":19900,\"20\":22000,\"21\":24450,\"22\":26900,\"23\":30000,\"24\":32500,\"25\":36100}",

JSON file for the second example

"{\"TotalSpeed\":0,\"SingleSpeed\":0,\"NoDQ\":0,\"ConsecutiveWins\":0,\"TotalRating\":0,\"TotalWins\":0,\"FriendWins\":0}"

The method I'm using to convert the title data is

	private void ExtractJsonData<T>(Dictionary<string, string> resultData, string titleKey, ref T output)
	{
		if (!resultData.TryGetValue(titleKey, out _))
			Debug.LogError("Failed to load titleData: " + titleKey);
		try
		{
			output = JsonConvert.DeserializeObject<T>(resultData[titleKey]);
		}
		catch (Exception e)
		{
			Debug.LogError("Failed to load titleData: " + titleKey);
			Debug.LogException(e);
		}
	}

Just to summarize, putting the user read only data result into this doesn't work but the title data result works just fine in this function. How can I save user read only data KVPs into a dictionary for easy access?

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

·
Citrus Yan avatar image
Citrus Yan answered

Hi David,

Just as your mentioned at the beginning, GetUserReadonlyDataResult is quite different from GetTitleDataResult. For GetUserReadonlyDataResult, it’s a dictionary with string as key and UserDataRecord as value, however, for GetTitleDataResult, it’s a dictionary with string as both key and value. Maybe you could try to convert the GetUserReadonlyDataResult to Dictionary<string, string> first and then put it into the ExtractJsonData function.

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

David Jones avatar image David Jones commented ·

I'm having errors converting the userdatarecord. Could you provide an example please?

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan David Jones commented ·

Hi David, I parsed every key-value pair in GetUserReadonlyDataResult to form a new dictionary<string, string>, this is the code I used to test:

Dictionary<string, string> newDictionary = new Dictionary<string, string>();
                    foreach (var keyValuePair in getUserDataResult.Data)
                    {
                        newDictionary.Add(keyValuePair.Key, keyValuePair.Value.Value);
                    }
                    Debug.Log("New dictionary:\n");

                    foreach (var keyValuePair in newDictionary)
                    {
                        Debug.Log("Key: " + keyValuePair.Key.ToString() + ", Value: " + keyValuePair.Value.ToString());

                    }

Does this meet your requirements?

0 Likes 0 ·
David Jones avatar image David Jones Citrus Yan commented ·

Yes that's awesome, I wasn't aware you could do what you wrote in line 2, thank you so much!

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.