Skip to content

Player Data

Jiufen edited this page Apr 2, 2021 · 3 revisions

We are going to take a look at the player data functionality of playfab. This player data, allow us to save an string info, so we have more posibilities to save the data of the player.

⏬Get Player Data⏬

Like the previous parts of the wiki, we will create a request for the player data and then we assign an onSuccess and an Onfailure callback:

public static void SetUserData() {
    PlayFabClientAPI.UpdateUserData(new UpdateUserDataRequest() {
        Data = new Dictionary() {
            {"InventorySize", "5"},
            {"Inventory0", "Sword"},
            {"Inventory1", "Shield"},
            {"Inventory2", "Bow"},
            {"Inventory3", "Spell"},
            {"Inventory4", "Potion"},
        }
    },
    result => Debug.Log("Successfully updated user data"),
    error => {
        Debug.Log("Error setting player inventory");
        Debug.Log(error.GenerateErrorReport());
    });
    GetUserData();
}

⏫Set player Data⏫

If hadn't any player Data for this player we proceed to create a new one:

public static void SetUserData() {
    PlayFabClientAPI.UpdateUserData(new UpdateUserDataRequest() {
        Data = new Dictionary() {
            {"InventorySize", "5"},
            {"Inventory0", "Sword"},
            {"Inventory1", "Shield"},
            {"Inventory2", "Bow"},
            {"Inventory3", "Spell"},
            {"Inventory4", "Potion"},
        }
    },
    result => Debug.Log("Successfully updated user data"),
    error => {
        Debug.Log("Error setting player inventory");
        Debug.Log(error.GenerateErrorReport());
    });
    GetUserData();
}

Clone this wiki locally