Sharing Data between Players

The GameSparks platform makes it easy for developers to be able to share game data between players. You may want to use this to implement features such as:

Ghost data replay in a racing game.

Sharing player-created game levels.

In this article, we'll work through an example of how to share ghost race data that would allow a game to display another player's previous attempt at a track/level within a simple racing game. To do this, we'll create Event and Cloud Code scripts within the GameSparks Developer Portal and then test the configuration using the Test Harness.

Important! If you're working on a new game that was created after the Game Data Service was launched in January 2018, you won't be able to create new Mongo Runtime collections and the Cloud Code used in the tutorial will not work. For the alternative tutorial for new games using the Game Data Service, see Sharing Data between Players Using Game Data Service.

Key Concepts

We will need to create an Event which we can use to post the ghost data into GameSparks when a player completes a race on a given track. This Event will have a Cloud Code script attached to it that stores the ghost data in the Mongo database. A second Event and Cloud Code script will be used to allow the game to query the ghost data collection for a given track and retrieve the player's ghost that represents the fastest time on this track.

For the purpose of this article we will assume that the ghost race data can be represented by the following JSON document containing an array:

The tick field is the game's animation counter.

The xPosition and yPosition are the coordinates of the player in the game world.

The heading field represents the direction that the player is facing within the game world.

3. Now, navigate to the NoSQL Explorer and under Collections expand Runtime and select script.raceData.

4. Click Find:

You'll see the document that was saved as a result of our Cloud Code script executing when the Event was received - this is returned into the Output panel.

You can click on the document to expand, edit, or delete it.

5. Repeat the sending of the Store race data Event but change the data each time, especially the TIME field. This will result in several documents being stored in MongoDB for PlayerN1.

6. Now register a second player, PlayerN2, by sending a RegistrationRequest and repeat the sending of the Store race data Event. Change the data each time, especially the TIME field. This will result in several documents being stored in MongoDB for PlayerN2.

Creating the Event and Cloud Code Script for Retrieving Ghost Data

Now we need to create an Event that allows the game to retrieve the fastest player's JSON ghost race data for a given track name.

Creating the Data Retrieval Event

1. Navigate to Configurator > Events.

2. Click to Add and a new Event.

3. Set up the Event as follows:

4. Click to Save and Close the new Event.

Adding Cloud Code to the Data Retrieval Event

Next we create the Cloud Code script to retrieve the data from the MongoDB collection and return it in the LogEventResponse.