Accessing scriptData in Unity
Some of our Unity users can have issues with script Data in Unity. This tutorial is written for the benefit of those Unity users and explains how to set scriptData in a request, retrieve it in the response, and parse the data.
This tutorial contains the following sections:
- What is scriptData?
- Parsing Primitive Data Types
- Parsing JSON Objects and Arrays
- How to Parse Nested scriptData
What is scriptData?
You can use scriptData for custom data you want to add to a request where the request does not have all the information you require. For example, if you have a LogEventRequest and you want to get the player display name, you can add it to scriptData by defining a key/value pair - the key being a unique identifier and the value being the value we want to retrieve on the client:
In Unity, you can then retrieve this value by calling a new LogEventRequest and retrieving the scriptData. Within the scriptData, you can get the string value associated with the key PlayerName by using the GetString(key) function:
Parsing Primitive Data Types
This section offers an example of how to parse different primitive data types from scriptData in Unity. Each example shows the Cloud Code and the C# code in Unity. The Cloud Code setup is not explained in detail, because it should be pretty straightforward. In these examples, you'll see that we've declared variables as var for some of the data types. This is just a shortcut declaration - if you don’t use var, you'll need to cast your returned data as the data type you require. This is done in the Numbers section below.
Numbers - Integers, Floats, Doubles, Longs
For numeric values, depending on the number type, you use the corresponding Getter. For example, if you want a double value, you'll need to use GetDouble(), and so on. Long’s round numbers to their nearest whole number, so if you use a decimal number, it will convert it to closest whole number.
Parsing JSON Objects and Arrays
In this section, we'll cover more advanced data types, which are JSON objects and arrays. Each have a few more steps which combine the Getters for the primitive types.
To parse an object in Unity, we've defined a custom class in our SDK GSData that is used as the declaration type of JSON objects. When trying to access the attributes of an object, you first need to get the object. Then, to get the value of each attribute, use the relevant Getter function. For example, name is a string so to get the name value we use GetString(“name”):
In our SDK, we have Getters to parse arrays as lists in Unity for most of the primitive types. For an array that contains one type of data, use the relevant Getter in the form of Get
How to Parse Nested scriptData
If you have a complex JSON object and you need certain nested data in this object, it can be a difficult to determine how to get that data in scriptData. In this example to illustrate how to do this, we show how to get the animals which the members of Bob's family like. The best way to approach this is to break the structure into smaller chunks and go one level at a time:
In Unity, we start with getting the entire object and storing it in a variable obj. This contains the entire JSON object. For the next step, we want the family. The family is a list of objects, so we can store these into an object called familyList using GetGSDataList. Next, we need to iterate through the list and extract the likes, which is another JSON object so we use GetGSData. Finally, we can get the value of the animal that each family member likes which is a string so we use GetString.