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:
- Creating a Game Data Type
- Creating an Event
- Writing the Cloud Code to query GDS.
- Write the Unity Code to send a request to GDS and retrieve the results.
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:
- In the Test Harness, authenticate a user and then send a LogEventRequest for the Event you created above. In the response, you'll see the returned player data:
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:
- Set the key, which is the Short Code of the QueryGDSUnity Event we created.
- Set the Attributes for that Event, which in this case is index and value.
- In the response, parse the scriptData to store locally into a variable in Unity. For a detailed explanation on how to parse scriptData in Unity see the following Accessing scriptData in Unity tutorial. Here, we return a list of objects with the key "data" then loop through the list extracting individual elements from the object.
- Here's a snippet of the required C# Code: