question

terrypapamarkou avatar image
terrypapamarkou asked

best practice for syncing player data

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

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

1 Answer

·
comc avatar image
comc Deactivated answered

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.

https://playfab.com/limits/

1 comment
10 |1200

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

terrypapamarkou avatar image terrypapamarkou commented ·

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.

1 Like 1 ·

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.