How to Create and Use Cloud Code

In this tutorial, you can follow an example and learn how to create and work with Cloud Code:

Cloud Code Best Practices! Before you start to build out the Cloud Code for your game, we strongly recommend that you read the Cloud Code Best Practices guide. This will help you to avoid creating Cloud Code structures that might compromise your game's performance and suggests ways in which you can check and review your Cloud Code's performance and efficiency.

Create an Event

1. Go to Configurator > Events and click to Add an Event. The Add Event page appears.

2. Enter the Event details - Short Code, Name, and Description - and click to Save and Close:

You are taken back to the Events page where the new Event is listed.

3. To edit the new Event, click the Edit icon. The Edit Event page appears.

4. Next, on the Attributes panel click to Add an Event Attribute.

5. Enter the Event Attribute details:

6. Click to Save the Attribute.

7. Repeat this step and create another two Attributes for this Event, one with Number for its Data Type, the other with JSON for its Data Type.

8. Click to Save and Close the editing changes you've made for the Event.

Create a Cloud Code Script

In this section, we'll create a Cloud Code script linked to the Event that we've just created.

Cloud Code Editor Shortcuts? For a list of the keyboard shortcuts you can use in the Cloud Code Editor, go here.

9. Go to Configurator > Cloud Code.

10. Select Events in the Scripts section.

11. Select the Event we've just created - CC_EVT. We'll now bind Cloud Code to it.

12. Copy and paste the following JavaScript code into the editor section.

    var eventAttr1 = Spark.getData().CC_ATTR
    var eventAttr2 = Spark.getData().CC_ATTR_2
    var eventAttr3 = Spark.getData().CC_ATTR_3
    Spark.setScriptData("eventAttr1", eventAttr1.toUpperCase());
    Spark.setScriptData("eventAttr2", eventAttr2 * 10);
    eventAttr3.won = true
    Spark.setScriptData("eventAttr3", eventAttr3);

13. Click Save at the bottom left-hand corner of the editor. Your page should now look like this:

Let's review this Cloud Code script:

We're now ready to test the code via the Test Harness.

Testing the Cloud Code Script and Event

In this section, we'll:

Register Test players

1. Go to Test Harness > Authentication and select RegistrationRequest.

2. Then change the displayName, password, and userName as shown here:

3. To send this JSON request to the GameSparks platform, click Send Request. In this example for Player Ten:

The Inspector shows the request (in green text) that was sent to the GameSparks platform via the WebSocket and shows the response (in blue text).

Log Event

4. Go to Test Harness > LogEvent section and select the Cloud Code Event that we created above.

5. Set each of the Attribute values in the JSON request as below. Note that:

6. Click the Send Request to send the request to the GameSparks platform:

The response is displayed in the Inspector in blue text:

Constructing a Script Message

Here's an example of how to construct a script message:

//Parameter is kept null if no custom script message is being used
var msg = Spark.message(null);
//Include player IDs of players you wish to receive message
msg.setPlayerIds([Spark.getPlayer().getPlayerId()]);
//Set the data object for the message
msg.setMessageData({"title":"I am testing title","body":"I am testing body"});
//Send message
msg.send();

To learn more about constructing messages and seeing an example use, please check out the chat tutorial.

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