question

Leonard Schultze avatar image
Leonard Schultze asked

Can anyone give me a code example for Player Data(publisher)?

I don't understand how to set player data(publisher) and then how other players can retrieve it.

Player Data
3 comments
10 |1200

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

JayZuo avatar image JayZuo ♦ commented ·

Could you please tell us which SDK or platform you are using?

0 Likes 0 ·
Leonard Schultze avatar image Leonard Schultze JayZuo ♦ commented ·

I use linux

0 Likes 0 ·
Leonard Schultze avatar image Leonard Schultze JayZuo ♦ commented ·

playfabSDK? playfabSDK: 2.116.211012

0 Likes 0 ·
Sarah Zhang avatar image
Sarah Zhang answered

It looks you are using PlayFab Unity SDK, is it right? If so, we will provide the code sample for your Unity project.

Besides, for clarification, the Player Publisher Data cannot be accessed by other players by default. Whether a type of data can be read by others doesn’t depend on whether the data is player publisher data or player title data. It depends whether the permission of the data is public or private. And also to clarify, the player publisher data can be associated with the player master account. It means the same player can own the player publisher data across titles under the same studio. The player title data means the data can be associated with the player title account. It means the same player can only own the player title data in the specific title. Please check the documentations associated with Play Data for more information.

So, if you want let other players can access one player’s data, you would need to set the permission of the data as public. And for your case, if you set the permission of player publisher data, other players will be able to modify it. If you only want to give the player and others read-only permission for the data, please use player publisher read-only data.

You can follow the sample code below to set up the player publisher data and set the data permission as public, so that other players can read the data using the client API GetUserPublisherData, and modify the data using the client API UpdateUserPublisherData. You can also check the documentation - How to use player publisher data to grant a reward for playing multiple titles - PlayFab | Microsoft Docs to check the official sample code about of the usage of player publisher data.

using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;


public class UpdatePublisherData : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        var request = new LoginWithCustomIDRequest() { TitleId = "[YourTitleId]", CustomId = "123" };
        PlayFabClientAPI.LoginWithCustomID(request, OnSucccuss, OnFailure);
    }


    private void OnSucccuss(LoginResult obj)
    {
        var request = new UpdateUserDataRequest() { Data = new Dictionary<string, string>() { { "Class", "Fighter" } }, Permission = UserDataPermission.Public };
        PlayFabClientAPI.UpdateUserPublisherData(request, OnUpdateSuccess, OnFailure);
    }


    private void OnUpdateSuccess(UpdateUserDataResult obj)
    {
        Debug.Log("Success.");
    }


    private void OnFailure(PlayFabError obj)
    {
        Debug.Log(obj.GenerateErrorReport());
    }



}
5 comments
10 |1200

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

Leonard Schultze avatar image Leonard Schultze commented ·

Yes i use unity and i can also set the player data to a value and retrieve it from the same accound but other players can't retrieve it then

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Leonard Schultze commented ·

As the above code said, you need to specify the permission of the update data request. Specifically, it would be

Permission = UserDataPermission.Public 

in the request definition

var request = new UpdateUserDataRequest() { Data = new Dictionary<string, string>() { { "Class", "Fighter" } }, Permission = UserDataPermission.Public };

So, other players can access the data.

0 Likes 0 ·
Leonard Schultze avatar image Leonard Schultze Sarah Zhang commented ·

Yes, I have permission for the public, but other players still can't access it can you give me a code example to retrieve the player datas?

0 Likes 0 ·
Show more comments
Sarah Zhang avatar image
Sarah Zhang answered
@Leonard Schultze

Could you please provide the code you are using currently to set the player publisher data for our reference?

10 |1200

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

Leonard Schultze avatar image
Leonard Schultze answered

here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine.UI;


public class PlayfabManager : MonoBehaviour
{
    [SerializeField] Text PlayerDataText;
    void Start()
    {
        var request = new LoginWithCustomIDRequest() {TitleId = "85980", CustomId = SystemInfo.deviceUniqueIdentifier, CreateAccount = true};
        PlayFabClientAPI.LoginWithCustomID(request, OnSucccuss, OnFailure);
    }
 
 
    private void OnSucccuss(LoginResult obj)
    {
        //In the Unity editor I run this: 
        SetPlayerData();
        //and with another account I run this:
        GetPlayerData();
    }

    void SetPlayerData()
    {
     var request = new UpdateUserDataRequest() { Data = new Dictionary<string, string>() { { "Class", "Fighter" } }, Permission = UserDataPermission.Public };
    PlayFabClientAPI.UpdateUserPublisherData(request, OnUpdateSuccess, OnFailure);
    }
 
    void GetPlayerData()
    {
    PlayFabClientAPI.GetUserPublisherData(new GetUserDataRequest(), OnDataGet, OnFailure);
    }


    void OnDataGet(GetUserDataResult result)
    {
    Debug.Log("Data: " + result.Data["Class"].Value);
    PlayerDataText.text = "Data: " + result.Data["Class"].Value;
    }


    private void OnUpdateSuccess(UpdateUserDataResult obj)
    {
        Debug.Log("Success.");
    }
 
 
    private void OnFailure(PlayFabError obj)
    {
        Debug.Log(obj.GenerateErrorReport());
    }
}
10 |1200

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

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.