Saving and Loading Player Data in Unity Using Game Data Service

In this tutorial, we're going to show you how to save and load player data in Unity using the Game Data Service. We'll cover this in four sections:

Read First! If you are unfamiliar with using Game Data Service, we strongly recommend that you preview the Game Data Service documentation, which can be found here, before you attempt to follow this tutorial.

Creating the Save and Load Events

First, we need to create custom Events that corresponds to each of two key operations - for saving the data to the Game Data Service and for loading the data into Unity.

To create the Save Event:

1. Go to Configurator > Events. The Events page opens.

2. Click Add.

3. Enter a Short Code, Name, and Description for the new Event.

4. Add one Attribute for this Event - playerdata with a type of JSON and Default Aggregation Type as Used In Script.

5. Click to Save and Close:

To create the Load Event:

1. Repeat steps 1-3 above.

4. For the Load event we don't need any Attributes, so leave this panel empty.

5. Click to Save and Close:

Writing the Cloud Code for the Save and Load Events

Now that we've created our Save and Load Events, we need to write the Cloud Code for each Event.

To configure the Cloud Code for the SavePlayerDataUnity Event:

1. Go to Configurator > Cloud Code > Events and select SavePlayerDataUnity.

2. In the Cloud Code editor, enter the following code for the Event. This code will be executed when we submit a LogEventRequest for this Event. Here we pass in some JSON data into the request and save the contents to our database:


To configure the Cloud Code for the LoadPlayerDataUnity Event:

1. Go to Configurator > Cloud Code > Events and select LoadPlayerDataUnity.

2. In the Cloud Code editor, enter the following code for the Event. This code will be executed when we submit a LogEventRequest for this Event. Here we retrieve the playerData document from our Data Type and pass it back to the client via scriptData:

Writing the Unity Code to Save the Player Data

Now that we have our Cloud Code written for our save and load Events, we can add our client side code in Unity.

1. Create a custom serializable PlayerData object. We'll need this because our input for the SavePlayerDataUnity LogEventRequest is JSON data. The data we are going to save are the player's name, job title, gold and the player inventory which is a List of strings.

2. Create a new playerData object and populate with some data.

3. Convert the playerData object into a JSON string using Unity's JSON serialization class.

4. Convert the JSON string into GSData using the GSRequestData class. The GSRequestData acts as a wrapper class to convert JSON data into a format that GameSparks understands.

5. Create a new LogEventRequest and set the key to SavePlayerDataUnity and add an Event Attribute that is the JSON data

Writing the Unity Code to Load the Player Data

1. Create a new LogEventRequest and set the event key to LoadPlayerDataUnity.

2. Parse the response's scriptData as GSdata:

Did this page help you? Please enter your feedback below.