Cloud Save for Cloud Code tutorial
The Cloud Save SDK for Cloud Code allows you to read and write data for Player, to read and write Game Data and to query data (if you have configured indexes to make data queryable) from Cloud Code.
Refer to the Cloud Save SDK for Cloud Code documentation for a list of all available methods.
Accessing Cloud Save from Cloud Code
The following is an example of how you can write to Player Data from a Cloud Code script.
Javascript
const _ = require("lodash-4.17");
const {
DataApi
} = require("@unity-services/cloud-save-1.4");
const NUMBER_OF_SIDES = 6;
module.exports = async ({ params, context, logger }) => {
const {
projectId,
playerId
} = context;
const api = new DataApi(context);
const roll = rollDice(NUMBER_OF_SIDES);
if (roll > NUMBER_OF_SIDES) {
// Log an error message with information about the exception
logger.error("The roll is greater than the number of sides: " + roll);
// Return an error back to the client
throw Error("Unable to roll dice!");
}
// Return a JSON result to the client
const result = {
sides: NUMBER_OF_SIDES,
roll: roll,
};
const setResult = await api.setItem(projectId, playerId, {
key: "DIE_ROLL",
value: result
});
logger.debug(JSON.stringify(setResult.data));
return result;
};
// Function outside of the script wrapper
function rollDice(sides) {
return _.random(1, sides);
}
Cross-Player Data
By passing another player’s ID to the Cloud Save SDK, you can read and write their Cloud Save data, allowing asynchronous multiplayer interactions. This would not be possible from within the client, where users are only permitted to access their own data.
The following example shows how to provide a commendation to another player within the same lobby. The sample code demonstrates accessing and modifying another player's data stored in Cloud Save. The otherPlayerId
parameter identifies the other player.
Javascript
const { DataApi } = require('@unity-services/cloud-save-1.4');
module.exports = async ({ params, context, logger }) => {
const { projectId } = 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 }
)
};
Requests originating from your players should be considered untrusted input, so be careful when accepting another player’s ID as a parameter, as in the above example. If your game has restrictions on when or how players can interact with each other (for example, if your players can opt out of multiplayer), then you need to check for those prerequisites within the script.
Use cases and examples
Explore other use cases and examples for the Cloud Save SDK with Cloud Code scripts.
Topic | Description |
---|---|
Quest System | Server authoritative / anti-cheat quest system. Cloud Code Modules (C#) |
Redeemable coupons | Offer players in-game awards. Cloud Code Script (JavaScript) |
Server-time anti-cheat | Implement an anti-cheat system. Cloud Code Script (JavaScript) |