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
Answer by comc · Apr 25, 2018 at 11:44 PM
You are right, constantly sending updates for high frequency changes to Playfab is not a good idea. So batching up those changes is smart. You might want to up the update time to 30 seconds, 10 seconds is almost nothing.
Make sure you are only updating items that you absolutely have to persist.
If you are performing logic and saving the steps, don't or consider running the steps on a server.
If your updates are too high frequency you may need to look at a custom server
You should also check that the update rates are not exceeding your account limits for number and frequency of updates.
Thank you for the feedback. I've adjusted my code so that it sends less frequently. If I'm reading the limits correctly I should be well within the allocations.
UserPrivateAccountInfo should not be sent when email is not available 1 Answer
Server GetPlayerCombinedInfo to get also user internal data 1 Answer
How can I get Photon clients to see if another player is premium? 1 Answer
How should I define in the catalog for buying players for a cricket game? 1 Answer
Data Access of Other Players 1 Answer