Hi,
Apologies if this has been asked before but I'm new to PlayFab and want to make sure I'm doing everything correctly. I'm making an iOS game in unity that has gold, gems, collectables etc. These numbers can fluctuate quickly and I noticed that the data in playfab would often be different to my local values if a variable was changed numerous times in quick succession. I then changed the code so that every time data is changed locally it adds the change a dictionary and this dictionary is only sent off to PlayFab if
a) the dictionary has 10 items
b) 10 seconds has passed since the last send
c) the app is paused (no longer the active app on the device)
Dictionary<string, string> dataToSend; void Awake(){ dataToSend = new Dictionary<string, string>(); StartCoroutine(SendDataRecurring()); } public void SetData(string key, string val) { if(dataToSend.ContainsKey(key)){ dataToSend[key] = val; }else{ dataToSend.Add(key, val); } if(dataToSend.Count == 10){ SendDataNow(); } } IEnumerator SendDataRecurring(){ while(true){ yield return Yielders.Get(10f); SendDataNow(); } } public void SendDataNow(){ if(dataToSend.Count > 0){ PlayFabClientAPI.UpdateUserData(new UpdateUserDataRequest() { Data = dataToSend }, result => Debug.Log("Successfully updated data"), error => { Debug.Log("Got error in SetData"); Debug.Log(error.GenerateErrorReport()); }); dataToSend.Clear(); } }
If the player leaves and then comes back, my plan is to then sync their data before the game starts, with something like this
public void DoSync(){ isSyncing = true; PlayFabClientAPI.GetUserData(new GetUserDataRequest() { PlayFabId = playerID, Keys = null }, result => { Debug.Log("Got user data:"); isSyncing = false; if (result.Data != null ){ foreach (KeyValuePair<string, UserDataRecord> kvp in result.Data) { Debug.Log("playfab data for "+kvp.Key +" was "+kvp.Value); //Save to player prefs here } } if(OnSyncCompleted != null){ OnSyncCompleted(SyncStatus.Success); } }, (error) => { isSyncing = false; Debug.Log("Got error retrieving user data:"); Debug.Log(error.GenerateErrorReport()); if(OnSyncCompleted != null){ OnSyncCompleted(SyncStatus.Error); } }); }
Does that all seem logical? Can you suggest any improvements?
Thanks in advance