Querying Game Data in Unity

In this tutorial, we're going to show you how to query in Unity the game data that you've stored using the Game Data Service. We'll be doing this using a custom Event with Cloud Code attached for issuing a LogEventRequest to query against a single index. To explain how to do this, we'll cover:

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 a Game Data Type

To create indexes that you can use to query the custom data in the Game Data Type:

1. Go to Data Explorer > Configure Indexes. This redirects you to the Configurator > Game Data page.

2. Click Add.

3. On the Add Data Type Index page, enter a Short Code, Name, and Description for the Data Type to which you want to add query indexes.

For this example, we'll be storing role-playing game (RPG) player data, so we've added an index to query against player class.

4. Click to Save and Close the query index you've added for the new Data Type.

To test your query, you'll now need to insert some data into your new Data Type.

5. Go back to the Data Explorer.

6. In the Data Types panel, select the Data Type for which you've just added a query index. The Data Type opens.

7. Click Insert.

8. Enter an ID for the document you're going to insert into your Data Type.

9. Under Data in the editor, enter some JSON data. In this case, we've inserted a first data document into our PlayerUnity Data Type.

Creating an Event

The Game Data Service doesn't have a pre-configured request that Unity can make directly, so we'll need to create a custom request that will handle querying the Game Data Service. We'll do this by creating an Event, attach Cloud Code to that Event, and then use the Event to issue a LogEventRequest.

To create an 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 two Attributes for the Event - index and value with both of type String and Default Aggregation Type as Used In Script.

For this example, we're going to pass in a single index to query against and the value of the data we want. For instance, we want to query for all classes that are equal to knight.

Writing the Cloud Code

Now that we've created our Event for a LogEventRequest, we need to write the Cloud Code for the Event to retrieve data from our Data Type. Below is a snippet we've used to loop through the Data Type and retrieve all items that match the query. When we get all the items, we store it as scriptData in order to parse the data on the client:

Before testing this in Unity, we recommend that you first test it in the Test Harness to make sure there are no errors and everything is working correctly:

Writing the Unity Code

Now that everything is working, we can start writing our Unity code.

1. Go to Unity and create a new LogEventRequest:

2. To send a LogEventRequest, you need to: