question

a0162hm avatar image
a0162hm asked

Get user data before saving it

Hello guys, im trying to add player statistics, when the game ends, i want to get the value what its currently set to so that i can compare the values, lets say in game 1 i reach wave 10, in game 2 i reach wave 9, first i want to check if wave 9 value is greater than the previous one, if it is, i just save the new value as is. if its not, i set the value to the previous one (so the 9 becomes 10) and then save it, so that it will always save the highest value.

however.. when trying to grab the values before they are made, i get an error and the function stops at that point. so it will never save it thus never add the new key.

I get that i cant grab a value of a key that doesnt exist yet, so how can i check if the key exists and if it doesnt, add it manually?

    public void OnEndGame()
    {
        // When the game ends, get all the playerdata
        GetPlayerData();
    }


    public void GetPlayerData()
    {
        PlayFabClientAPI.GetUserData(new GetUserDataRequest(), OnDataReceived, OnError);
    }


    public void OnDataReceived(GetUserDataResult result)
    {
        // the current wave the player lost at
        int tempWave;
        tempWave = WaveSpawner.currentWave;


        // highestwavekey = "highestwavemap1"
        if(result.Data != null && result.Data.ContainsKey(highestWaveKey))
        {
            // setting this string to the previous value, the highest wave the player reached before
            newHighestWave = result.Data[highestWaveKey].Value;
            
            // convert the string to an int
            int.TryParse(newHighestWave, out highestWave);
        }


        // if the current wave is smaller than my previous record,
        // it will set the current wave int to the previous one,
        // so that it will always save the highest value
        if(tempWave < highestWave)
        {
            tempWave = highestWave;
        }


        // here im saving the new value, if the value is higher than before, it will be the new high score
        var request = new UpdateUserDataRequest
        {
            Data = new Dictionary<string, string> {
                {highestWaveKey, tempWave.ToString()}  // save value
            }
        };
        PlayFabClientAPI.UpdateUserData(request, OnDataSend, OnError);
    }

ERROR: KeyNotFoundException: The given key 'highestwavemap1' was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <6073cf49ed704e958b8a66d540dea948>:0)
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.

a0162hm avatar image
a0162hm answered

Okay so the problem was that i had 1 if statement that checked if results.data.containskey(key1) || key 2 || key 3 || key 4 etc and then it grabs the values of all 4 keys, BUT if 1 key doesnt exist, it will still try and grab the values which causes the error, so instead i used && so the key HAS to exist before getting the values, but that again raises another question, what if i try to add a key later on? yeah so if 1 key doesnt exist, it will skip the whole if statement and save the current values WHICH IS NOT GOOD, example:

highscore : 5, in game 2 i add another key and in the same game i get 2 points, well since its checking for all the keys to exist, it will skip the part where it grabs the data and compare the value 5 and 2 and just move on to the saving part, resulting in the highscore being set to 2 even though it has to be 5.

for that i made an if statement for each key separately, so if 1 key doesnt exist, it wont stop the code there and others can still get their values, the values will also be saved once again so that is where the key is made if it didnt already exist

        if(result.Data != null && result.Data.ContainsKey(mostKillsAchievedKey))
        {
            newMostKills = result.Data[mostKillsAchievedKey].Value;
            int.TryParse(newMostKills, out mostKillsAchieved);
        }

        if(result.Data != null && result.Data.ContainsKey(totalKillsKey))
        {
            newTotalKills = result.Data[totalKillsKey].Value;
            int.TryParse(newTotalKills, out totalKills);
        }

        if(result.Data != null && result.Data.ContainsKey(highestWaveKey))
        {
            newHighestWave = result.Data[highestWaveKey].Value;
            int.TryParse(newHighestWave, out highestWave);
        }

        if(result.Data != null && result.Data.ContainsKey(totalDownsKey))
        {
            newDowns = result.Data[totalDownsKey].Value;
            int.TryParse(newDowns, out totalDowns);
        }

        if(result.Data != null && result.Data.ContainsKey(totalRevivesKey))
        {
            newRevives = result.Data[totalRevivesKey].Value;
            int.TryParse(newRevives, out totalRevives);
        }

10 |1200

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

Gosen Gao avatar image
Gosen Gao answered

According to your description, I think Tournaments & Leaderboards - PlayFab | Microsoft Docs is what you need. You can create a Leaderboard with “Aggregation method” setting to “Maximum” to always use the highest value of the Statistic. For more info about using Leaderboards, please refer to Tournaments & Leaderboards quickstart - PlayFab | Microsoft Docs.

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

a0162hm avatar image a0162hm commented ·

hello, i'm not trying to create a leaderboard, the aforementioned highscore value is just for the player's personal stats to see his progress etc. other people won't be able to see it.

game 1 : 100 points (highscore = 100)

game 2 : 40 points (highscore = 100)

game 3 : 120 points (highscore = 120)

0 Likes 0 ·
Gosen Gao avatar image Gosen Gao a0162hm commented ·

Sorry for the misunderstood. I have tested your code, and it works fine, it creates a new key when it is not exist. May I know in which line did the error occur?

0 Likes 0 ·
a0162hm avatar image a0162hm Gosen Gao commented ·

post updated

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.