Cloud Code

Introduction

The Configurator > Cloud Code page lists all the potential interception points that Cloud Code can be bound to in the Scripts panel and has a JavaScript editor where you can write your interception code.

Scripts

The Scripts section is split into separate categories, which are further explained below. You can expand a category to select an individual item to which you want to attach your Cloud Code script, such as an Event or Request.

There are five main option buttons at the bottom of the Scripts panel:

Accessing Cloud Code

To access the Cloud Code JavaScript editor, select the name of the script from one of the ten categories in the Scripts section. Use the Save, Close, and Delete buttons at the bottom of the editor to manage your scripts:

Keyboard Shortcuts to Save! You can also save your scripts using standard key press combinations, CMD+S on OSX and CTRL+S on Windows and Linux.

Keyboard Shortcuts List! For a list of keyboard shortcuts that you can use in the Cloud Code Editor see below

Refreshing the Scripts Panel

For some Scripts categories, you can use the plus icon to quickly add a new item, such as adding a new Event. This saves you having to navigate away from the Cloud Code page and then come back to the page to add your Cloud Code script.

However, you might be working on your game in several tabs at once or you might be developing your game along with several Collaborators. If you've added a new Event using the plus icon in one tab and switch to another, you must click the refresh icon button to see the added Event in the Script list. Similarly, if one of your Collaborators has added an Event, you must refresh the Scripts panel to see that Event.

Cloud Code Tutorials? For a rich set of tutorials on how to work with Cloud Code, go here.

Scripts Categories

Click to expand any of the Scripts categories and select the interception point to which you want to attach your 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.

Events

The Events list contains an entry for each Event you have created within the GameSparks platform:

Challenge Events

The Challenge Events list contains an entry for each Event you have created within the GameSparks platform:

    var myChallenge = Spark.getChallenge(Spark.data.challengeId);

Challenge Turn Taken Messages! Note that if you submit a LogChallengeEventRequest, then a ChallengeTurnTakenMessage will be automatically generated. This happens to ensure that you are notified if any change to the Challenge is effected by the request. For more details on messaging for Challenges, see the Understanding and Testing Challenges tutorial.

Requests

The Requests list contains an entry for each Request you can call within the GameSparks platform:

Adding Common Functionality! LogEventRequest and LogChallengeEventRequest are global scripts that will be executed for these request types before a specific script is called for each Event. This allows you to add a common functionality to all Events.

Responses

The Responses list contains an entry for each Response that can be returned from the GameSparks platform:

User Messages

The User Messages list contains an entry for each Message that can be returned from the GameSparks platform:

Global Messages

The Global Messages list contains an entry for each Message that can be returned from the GameSparks platform:

System

The System tab contains a number of System Events that are able to trigger some JavaScript Cloud Code:

Callbacks

Modules

Modules allow you to create your own libraries of JavaScript that can be included within other scripts. This allows you to separate common functionality that needs to be shared between scripts into a single module that can be included.

    require("MODULE_SHORT_CODE");
    if(something) {
        require("MODULE_SHORT_CODE_1");
    } else {
        require("MODULE_SHORT_CODE_2");
    }

Realtime Scripts

Realtime Scripts are listed here:

Realtime Modules

Realtime Modules allow you to create your own libraries of JavaScript that can be included within other Realtime scripts. This allows you to separate common functionality that needs to be shared between scripts into a single Realtime module that can be included.

Realtime modules use CommonJS module loading. A primer for CommonJS can be found here.

To author a Realtime module, you must define the objects you want to export. This is done using "module.exports". You can export multiple items from a single module:

module.exports.fiz = function () {
    console.log('fiz!');
}

module.exports.buzz = function () {
    console.log('buzz!');
}

To include the Realtime Module in a Realtime Script, the require function should be used as follows:

var theModule = require("MODULE_NAME");
var fizResult = theModule.fiz();
var buzzResult = theModule.buzz();

At this point, theModule represents the exports from the Realtime Module

Cloud Code Editor Keyboard Shortcuts

This section lists keyboard shortcuts you can use when working in the Cloud Code Editor.

Selection

Windows/Linux Mac Action
Ctrl+A Command+A Select All
Shift+Left Command+Left Select Left
Shift+Right Command+Right Select Right
Ctrl+Shift+Left Option+Shift+Left Select Word Left
Ctrl+Shift+Right Option+Shift+Right Select Word Right
Shift+Home Shift+Home Select Line Start
Shift+End Shift+End Select Line End
Alt+Shift+Left Command+Shift+Left Select to Line Start
Alt+Shift+Right Command+Shift+Right Select to Line End
Shift+Up Shift+Up Select Up
Shift+Down Shift+Down Select Down
Ctrl+Shift+Home Command+Shift+Up Select to Start
Ctrl+Shift+End Command+Shift+Down Select to End
Ctrl+Shift+D Command+Shift+D Duplicate Selection

Line Operation

Windows/Linux Mac Action
Ctrl+D Command+D Remove Line
Alt+Shift+Down Command+Option+Down Copy Lines Down
Alt+Shift+Up Command+Option+Up Copy Lines Up
Alt+Down Option+Down Move Lines Down
Alt+Up Option+Up Move Lines Up
Alt+Delete Ctrl+K Remove to Line End
Alt+Backspace Command+Backspace Remove to Line Start
Ctrl+Backspace Option+Backspace, Ctrl+Option+Backspace Remove Word Left
Ctrl+Delete Option+Delete Remove Word Right
--- Ctrl+O Split Line
Ctrl+M+/ --- Comment/Uncomment Line

Go To

Windows/Linux Mac Action
Ctrl+L, Ctrl+M+L Command+L, Command+M+L Go to Line
Left Left, Ctrl+B Go to Left
Right Right, Ctrl+F Go to Right
Ctrl+Left Option+Left Go to Word Left
Ctrl+Right Option+Right Go to Word Right
Up Up, Ctrl+P Go to Line Up
Down Down, Ctrl+N Go to Line Down
Alt+Left, Home Command+Left, Home, Ctrl+A Go to Line Start
Alt+Right, End Command+Right, End, Ctrl+E Go to Line End
Ctrl+Home Command+Home, Command+Up Go to Start
Ctrl+End Command+End, Command+Down Go to End
Ctrl+Down Command+Down Scroll Line Down
Ctrl+Up --- Scroll Line Up

Find/Replace

Windows/Linux Mac Action
Ctrl+F, Ctrl+M+F Command+F Find
Ctrl+H Command+Option+F Replace
Ctrl+K, Command+G Find Next
Ctrl+Shift+K Command+Shift+G Find Previous
Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here