Hello I am having an issue I am using unity cloud save to save and load players data it is working perfectly but I want to access other players data using player id how is it possible??
Hi Ali,
At the moment the only way to retrieve another player’s data would be to use a Cloud Code script, as Cloud Code has admin permissions and can access any player’s data. The Cloud Save documentation includes an example of how to do this.
We’re also currently looking at improvements to allow for more diverse access controls in Cloud Save (e.g. publicly readable player data), which might be of use to you in future depending on your use case!
@devingunity I see. Need like to save player profile picture to base64 for leaderboard use case. So current player when to look at other player could see their picture.
Could you give details? Will I still have to use cloud code?
I recommend you to not access other player Data with the id as using Cloud Code have admin permissions so when the people hacks your game they will get admin access and will be able to change other player values try to create login in some other way you like. But still if you want to access it use cloud code.
But if it is a remote query function only. Why would this allow malicious users to edit information from other players?
@EwertonBR here are steps:
Save user profile picture to Cloud Save
- Convert their picture to base64
- Save it to Cloud Save with key
user_picture(you can name the key whatever you want)
When you get list user of leaderboard, they will return many variables include PlayerID, we can use PlayerID to retrieve their picture on their Cloud Save. Because retrieving data Cloud Save from other user required project id which is data sensitive where client side should not have access, so we need create Cloud Code.
Cloud code steps:
- Create new script in Cloud Code name it
GetPlayerPicturewith parameter_playerID(you can name the script name and parameter name whatever you want) - Then use api
cloudSaveAPI.getItemswithprojectID,_playerIDand keyuser_picturewhere it will return array of items, so you just need access the first item of array. - Then return the value as json to deserialize from client side.
Example cloud code:
const _ = require("lodash-4.17");
const { DataApi } = require('@unity-services/cloud-save-1.3');
/*
* CommonJS wrapper for the script. It receives a single argument, which can be destructured into:
* - params: Object containing the parameters provided to the script, accessible as object properties
* - context: Object containing the projectId, environmentId, environmentName, playerId and accessToken properties.
* - logger: Logging client for the script. Provides debug(), info(), warning() and error() log levels.
*/
module.exports = async ({ params, context, logger }) => {
const { projectId } = context;
const { _playerID } = params;
// Initialize the cloud save API client
const cloudSaveAPI = new DataApi(context);
// Access the save data for the specified player
const otherPlayerSave = await cloudSaveAPI.getItems(
projectId,
_playerID, // or any other player ID
"user_picture" // Cloud Code key
);
const otherPlayer = otherPlayerSave.data.results[0] == null ? "" : resultOtherPlayer.value;
// Return the JSON result to the client
return {
Base64Picture: otherPlayer
};
};
Example to get other player picture using Cloud Code that we made.
private class Picture
{
public string Base64Picture;
}
/// otherPlayerID is
private async void GetPictureAsync(string otherPlayerID)
{
var args = new Dictionary<string, object>() { { "_playerID", otherPlayerID } };
var result = await CloudCodeService.Instance.CallEndpointAsync<Picture>("GetPlayerPicture", args);
/// then use result.Base64Picture to convert to texture2D.
}
@aliwebconsultant I believe it’s impossible to hack cloud code.
It’s true that it has admin right, but the Cloud Code cannot modified from client side.
Modified other player value also it’s impossible from client side too where it need project id and some api may required environment id and secret token.
So if you use Cloud Code to retrieve data only it’s impossible to get hacked.
That’s nice buddy :).
Very good! Thanks for answering.
Hi, for anyone still looking.
They have added the feature to read another player’s public data.
Please see the explanation in the docs Unity SDK tutorial
public async void LoadPublicDataByPlayerId()
{
var playerId = "JE1unrWOOzzbIwy3Nl60fRefuiVE";
var playerData = await CloudSaveService.Instance.Data.Player.LoadAsync(new HashSet<string>{"keyName"}, new LoadOptions(new PublicReadAccessClassOptions(playerId)));
if (playerData.TryGetValue("keyName", out var keyName)) {
Debug.Log($"keyName: {keyName.Value.GetAs<string>()}");
}
}