Amazon GameSparks is currently in preview. Changes might be made to this service and to this documentation. We don't recommend using this service for production workloads.
Building the Button Blaster example in Amazon GameSparks
In this topic you'll create a Button Blaster game in the Amazon GameSparks backend. The game records the clicks made by the player by incrementing a counter in the player data. You'll create the event raised by the click and a notification that results from the event. You'll also create a request that the client can make to get the count.
Note
Set up GameSparks before proceeding.
Create a GameSparks game
To create a GameSparks game
-
In the GameSparks navigation pane, select Games.
-
Choose Create Game.
-
For Game name enter
ButtonBlaster
. -
Choose Create and use new roles for each stage.
-
Choose Create.

Important
When you create your messages, make sure that you use exactly the same names given below for the messages and fields. Otherwise the game won't function because the generated code in the game client won't match the cloud code in the backend.
Create an event
As players play your game, the GameSparks backend handles the events that occur. Events can occur for anything you want: a portal is opened, an artifact is acquired, a team wins a battle, a player wins their 100th game. An event is one kind of message that your game client raises to the backend. After sending the event, the client continues to the next instruction; no response is expected.
To try a simple example, you'll create an event that occurs when the player chooses a button.
To create an event
-
In the navigation pane, select Dev, expand Configuration, and then choose Cloud code.
-
Choose Create message.
The Create message dialog box appears.
-
Choose Event
-
In the Name box, enter
ButtonPressed
. -
(Optional) enter a description.
-
Choose Create
The
ButtonPressed
message appears.
You just added an event to your game configuration. To make your event have an effect in your game, you add one or more handlers. In each handler you add cloud code to implement your game logic.
To add code to an event
-
Select Player.
-
Choose Add event handler
The Add event handler dialog box appears.
-
For the name, enter
ButtonPressHandler
. -
(Optional) Add a description.
-
Insert the following code into
ButtonPressHandler
.GameSparks().Logging().Debug("Inside ButtonPress event handler"); // Retrieve player data GameSparks().Logging().Debug("Retrieving player data"); let data = GameSparks().CurrentPlayer().GetData(["ButtonPressed"]); // Handle initial case with no data if (data.ButtonPressed === undefined) { data.ButtonPressed = 0; } // Increment the counter GameSparks().Logging().Debug("Incrementing button press counter"); data.ButtonPressed++; GameSparks().Logging().Info("Setting a value of button press count"); // Store player data GameSparks().CurrentPlayer().SetData(data);
-
Choose Save.
Create a notification
Create a notification that the backend sends to the client when the button is pressed.
To create a notification
-
In the console on the Cloud code page, choose Create message, and then choose Notification.
The Create message dialog box appears.
-
Enter the name
ButtonPressedNotification
. -
(Optional) Add a description.
-
Choose Create.
-
In the notification fields, for Field name enter
buttonPressCount
, for Shape selectInteger
, and for Required selectYes
. -
Choose Save.
You're ready to enable triggering of this notification by your ButtonPressed
event.
To enable triggering of a notification
-
Choose the
ButtonPressed
event in the Messages pane. -
Choose Add event handler and name it
NotificationHandler
. -
Insert the following code into
NotificationHandler
.GameSparks().Logging().Debug( "Inside ButtonPressed Notification dispatch handler"); let data = GameSparks().CurrentPlayer().GetData(["ButtonPressed"]); // Handle initial case with no data if (data.ButtonPressed === undefined) data.ButtonPressed = 0; GameSparks().Logging().Debug("Sending notification: " + data.ButtonPressed); GameSparks().CurrentPlayer().SendNotification( "Custom.Game.ButtonPressedNotification", { "buttonPressCount": parseInt(data.ButtonPressed) });
-
Choose Save.
Your ButtonPressed
event now triggers two outcomes defined in the code of two event handlers.
The ButtonPressHandler
increments the counter.
The NotificationHandler
alerts the game client.
Create a request
In addition to the notification above, create a request so the client can get the count when needed.
To create a request
-
In the GameSparks console, on the Cloud code page, choose Create Message.
The Create message dialog box appears.
-
Choose Request, and enter
GetButtonPressCount
as the name. -
(Optional) Enter a description.
-
Choose Create.
-
Select Player.
-
Add a response field named
buttonPressCount
, choose a Shape ofInteger
, and for Required chooseYes
. -
Add the following cloud code in the request handler
GameSparks().Logging().Debug( "Inside GetButtonPressCount request handler"); let data = GameSparks().CurrentPlayer() .GetData(["ButtonPressed"]); // Handle initial case with no data if (data.ButtonPressed === undefined) data.ButtonPressed = 0; return GameSparks().Messaging().Response( { "buttonPressCount": parseInt(data.ButtonPressed) } );
-
Choose Save.
Deploy your changes
You must deploy the game configuration in order to test your changes from the test harness or from the game client.
To deploy your changes
-
In the GameSparks navigation pane, select Dev.
-
Choose Deploy as new snapshot.
The Deploy as new snapshot dialog box appears.
-
Enter a description that will help you remember the changes you made. For example: Added messages to implement button press event. Client gets data via notification and request.
-
Choose Save.
A banner appears: Saving snapshot and deploying updates….
After the snapshot has been saved and deployed, its number appears in the form Snapshot_<timestamp>
along with your description.

Refresh your browser to get the Dev stage key to appear. Later you'll use this key to connect from the client to this stage of your game.
You just created a snapshot of your configuration, which includes the cloud code you wrote. You also deployed the snapshot to the dev stage of the backend so that your game clients can connect to it. The snapshot is also exportable, importable, and part of the history of your game.
Test your messages
With the test harness you can iterate much more quickly to develop and test your messages.
Note
If some of your events or requests don't appear, go back to the Cloud code page and make sure that Player is selected for each of your messages.
Your ButtonPressed
event does two things. It increments the score and it sends a ButtonPressedNotification
to the client. To test these:
To test your messages
-
In the GameSparks navigation pane, select Test harness.
-
Wait until the Player card is Connected.
-
Select the ButtonPressed message.
-
Choose Populate example.
-
Choose Send message.
-
The
ButtonPressed
event appears in the Log inspectorⓘ Event sent
Id: 1a071196-4f76-421a-90ee-d9a5ee8ba220 Type: Custom.Game.
ButtonPressed
Category: Event {} -
You will receive a
ButtonPressedNotification
with a payload like{"buttonPressCount":1}
.✓ Notification received
Id: d81c89cc-5fac-4392-91ad-4b71b97a6b97 Type: Custom.Game.
ButtonPressedNotification
Category: Notification {"buttonPressCount":1} -
You can choose Send message a few times to increment the score.
Your GetButtonPressCount
request enables the client to request the score whenever it needs it.
To test this request, choose GetButtonPressCount, choose Populate example, and then choose Send message.
Import the Button Blaster sample
Note
Make sure you've set up the client SDK in your Unity game before proceeding.
For this tutorial, your client code comes from a sample that is built into the GameSparks SDK.
To import the sample
In Unity, create a 3D project named
ButtonBlaster
.-
In the Unity editor window, from the Window menu, choose Package Manager.
-
Import the sample.
In Package Manager, choose Amazon GameSparks.
Expand Samples
Next to ButtonBlaster choose Import.
-
Save your Unity project.
Add the game key to your project
Your GameSparks game accepts a connection only if the game client provides the game key.
To add the game key to your Unity project
-
In GameSparks, navigate to the Dev page, and copy the key to your clipboard.
Note
If you don't see the key, make sure that you have chosen Deploy as new snapshot and refreshed the page.
-
In the Unity project pane, browse to and select the Connection asset in
/Assets/Samples/Amazon GameSparks/0.0.3/Button Blaster/UsingScriptableObjects/Assets/Connection
. -
In the Inspector pane, paste the key into the Game Key box.
Open the UsingScriptableObjects scene
In the Unity project pane, browse to and double-click the UsingScriptableObjects scene in /Assets/Samples/Amazon GameSparks/0.0.3/Button Blaster/UsingScriptableObjects/Assets/Scenes
.
Connect the client to GameSparks
To connect the client to GameSparks
In Unity, if the console is not already displayed, from the Window menu, choose General, Console.
From the Edit menu, choose Play.
-
In the Console, observe how the game connects by searching for key words in the search box.
Search for… | See a log entry like… |
---|---|
|
Connecting using gameKey: aBcDeFgHiJkLmNoPqRsTUvWxYz-aBcDeFgHiJkLmNoP and playerId: 0a0b0c0d-1e1f-2g2h-3i3j-4k4l4m4n4o4p |
|
received GetConnectionInfo response for player [playerId: 0a0b0c0d-1e1f-2g2h-3i3j-4k4l4m4n4o4p authToken: <authentication token string>, now connected |
Press the button
Choose the button to increment the score.
Note
To enlarge the view, near the top of the Unity window, drag the Scale slider to the right.

The data in the bottom of the scoreboard panel comes from the notification. The data in top of the panel comes from the response to the request.
The table below provides a simplified explanation of how the game works.
Behavior | Sequence |
---|---|
The user presses the button in the client, which raises the event. |
|
The client receives the notification that results from an event. |
|
The client makes a request. |
|
You can browse deeper into the code mentioned above in /Assets/Samples/Amazon GameSparks/0.0.3/Button Blaster/UsingScriptableObjects/Scripts/Player.cs
.
The lower-level code is in /Assets/Samples/Amazon GameSparks/0.0.3/Button Blaster/Scripts/Generated/ButtonBlasterOperations.cs
.
This file was generated from the backend to enable writing strongly-typed code to match the message shapes in the cloud code.
To learn about generating code, see Part 2: Unity game client
.
If you prefer to use code directly (instead of with a ScriptableObject), see /Assets/Samples/Amazon GameSparks/0.0.3/Button Blaster/UsingCode/Scripts/Player.cs
.