How does Server pull PlayerData from CouldSave

For my multiplayer game, I want to manage player data using CloudSave. My plan is to fetch the player data by the server and then set the network variables by the server to the client-class.

The provided unity-example shows how to get data for the authenticated player, but not for other players. Sending data from the client to the server seems unsafe to me.

Does my approach make sense, or am I entirely off track?

Additionally, my server has some global data that needs to be stored and shared across all servers. Where should I store this data?

You could make use of Cloud Code and access the player’s data from there, either via Javascript or C#.

You can have each client request the data that’s player specific. It’s only the writing part that you need to make sure goes through an authority, either the dedicated server or cloud code. You want to prevent players from storing items in their inventory out of thin air or something like that, but they can individually get these values directly from cloud save since they’ve already been verified to be correct.

Unless the server also needs to know these values that is.

This sounds like cloud save “custom items” (terrible name for “global data”).

However, if you want to write custom items at runtime there are limitations. You will also need to manage conflicts, like two servers trying to write the same value at the same time. It would be best to use cloud save to orchestrate writing values in a safe manner and resolving any conflicts.

Seems like what I want to do.

Im trying to get the provided example to run. But it errors with 422 Unprocessable Entity.
It looks like it can’t use the “otherPlayerId” parameter. otherPlayerId is null for some reason.

Edit: I forgot to define the Parameter in the CloudCode Script Properties.

That is how I called it from the Unity C# side

    var functionName = "test";
    var parameters = new Dictionary<string, object>
        {
            {"otherPlayerId", playerId}
        };

    await CloudCodeService.Instance.CallEndpointAsync(functionName, parameters);

This is the Cloud Code Example


const { DataApi } = require('@unity-services/cloud-save-1.4');

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId} = context;
  const { otherPlayerId } = 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,
    otherPlayerId, // or any other player ID
    "likes" // Cloud Code key
  );

  // Assume that player's likes is 0 if the key doesn't exist
  const otherPlayerLikes = otherPlayerSave.data.results[0]?.value || 0;

  // Update the data for the specified player
  cloudSaveAPI.setItem(
    projectId,
    otherPlayerId, { key: "likes", value: otherPlayerLikes + 1 }
  )
};

I got the examples to work.

I wanted the reading on the server-side so the server can implement the network variables used by the clients.

One more question: My game has in-game money. Is it smart to handle every transaction with cloud code instead of running it on the server? This would keep it perfectly synced, but would it create too much traffic for the server or incur high cloud fees?

Any tips on managing this?
Edit: I turned out using the EconomyService