I am using unity game service cloud save to backup important player data. This works great when I use it to save variable(s). However, I cannot figure out how to load variables without hardcoding each possible combination. I am confident there is something simple I am missing, but I do not know what it is, and the AI suggestions I get are off, likely because I am asking the wrong questions.
Please help?
Working function to load multiple variables to the cloud:
private async Task<string> CloudSaveMultipleValues(Dictionary<string, object> valuesToSave)
{
try
{
await CloudSaveService.Instance.Data.Player.SaveAsync(valuesToSave);
Debug.Log($"Successfully saved data to cloud");
}
catch (CloudSaveValidationException e)
{
Debug.LogError(e);
}
catch (CloudSaveRateLimitedException e)
{
Debug.LogError(e);
}
catch (CloudSaveException e)
{
Debug.LogError(e);
}
return null;
}
Example of the 'valuesToSave input:
allStats_dict = new Dictionary<string, object>
{
{ nameof(gm.stats_CloudSave.gameVersion), gm.stats_CloudSave.gameVersion },
{ nameof(gm.stats_CloudSave.numsPlaced), gm.stats_CloudSave.numsPlaced },
{ nameof(gm.stats_CloudSave.points), gm.stats_CloudSave.points },
{ nameof(gm.stats_CloudSave.gamesPlayed), gm.stats_CloudSave.gamesPlayed },
{ nameof(gm.stats_CloudSave.dateOfFirstGame), gm.stats_CloudSave.dateOfFirstGame }
};
The loading function I am trying to get to work:
private async Task<string> RetrieveSpecificCloudData(HashSet<string> keys)
{
try
{
var results = await CloudSaveService.Instance.Data.Player.LoadAsync(keys);
//this confirms I got all the keys I asked for
Debug.Log($"{results.Count} elements loaded!");
//this confirms the values are correct
Debug.Log($"numsPlaced: {results["numsPlaced"].Value.GetAs<int>()}");
Debug.Log($"dateOfFirstGame: {results["dateOfFirstGame"].Value.GetAs<DateTime>()}");
//what I cannot figure out is how to assign the value of each key to the appropriate variable???
//I can hardcode it like this, which works, but there has to be a better way so I do not have to hardcode so many different scenarios... (one for each combination of keys)
gm.stats_CloudSave.numsPlaced = results["numsPlaced"].Value.GetAs<int>();
//I need something here that loops through the input (keys), and assigns the value of each key to the appropriate variable. AI tools suggested using reflection, but I can't get it to work...
}
catch (CloudSaveValidationException e)
{
Debug.LogError(e);
}
catch (CloudSaveRateLimitedException e)
{
Debug.LogError(e);
}
catch (CloudSaveException e)
{
Debug.LogError(e);
}
return null;
}
Example of the 'keys' input Hashset:
HashSet<string> keys = new HashSet<string>
{ nameof(gm.stats_CloudSave.numsPlaced),
nameof(gm.stats_CloudSave.dateOfFirstGame)
};
Aucun commentaire:
Enregistrer un commentaire