How to Create and Use Game Specific Data

You might want to make game-specific data, either static data or global variables, available to every player or to requests made to the server. An example could be a global message of the day, or a notification to tell players they must have the latest version of the app in order to gain access to multiplayer features.

Declaring the Data

Javascript Constants

Using Constants in a Cloud Code script you can set some values to be returned to players. This would work well for values that don't need to change often since changing this script means having to publish a new game Snapshot.

To use Constants in your Cloud Code simply declare a variable with const instead of var. We use const because it's more efficient and also because the values aren't going to change:

//Creating an object with data to return back to the player
const data = {testNumber : 123, gameVersionNUmber : "v1234", gameMessageOfMonth : "Hey December is gonna be great!"};

//Return all data back with one setScriptData call
Spark.setScriptData("gameData", data)

//Alternatively you can declare each value as a variable
const testNumber = 123;
const gameVersionNUmber = "v1234";
const gameMessageOfMonth = "Hey December is gonna be great!";

//Return each value as key value pair
Spark.setScriptData("testNumber", testNumber);
Spark.setScriptData("Game Version Number", gameVersionNUmber);
Spark.setScriptData("GMOM", gameMessageOfMonth);

Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here