Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • API and SDK Questions /
avatar image
Question by TerryPapamarkou · Apr 23, 2018 at 05:50 PM · Player Data

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

Comment

People who like this

0 Show 0
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

https://playfab.com/limits/

Comment
TerryPapamarkou

People who like this

1 Show 1 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TerryPapamarkou · Apr 26, 2018 at 01:29 PM 1
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Navigation

Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    1 Person is following this question.

    avatar image

    Related Questions

    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

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges