I'm trying to send a JSON string as an EventAttribute to a cloud event so that I can set multiple field values at once and this is the cloud code I'm using...
var playerInventory = Spark.runtimeCollection("player_inventory");
var slotIndex = Spark.data.INDEX;
var slot = "SLOT_" + slotIndex.toString();
var inputValue = Spark.data.CONTENTS;
var updateObject = {};
updateObject[slot] = inputValue;
//Now do the mongo update.
playerInventory.update(
{"_id": Spark.getPlayer().getPlayerId()}, //Looks for a doc with the _id of the current player
{"$set" : updateObject}, // Uses the $set mongo modifier to set value at a path
true, // Create the document if it does not exist (upsert)
false // This query will only affect a single object (multi)
);
Spark.setScriptData("data", inputValue);
In the test harness it works perfectly, if I enter something like this...
public void SaveSlotContentsToCloud(int index, InventorySlot slot)
{
new GameSparks.Api.Requests.LogEventRequest()
.SetEventKey("SET_SLOT_CONTENTS")
.SetEventAttribute("INDEX", index)
.SetEventAttribute("CONTENTS", JsonUtility.ToJson(slot))
.Send(response => {
if(!response.HasErrors)
{
Debug.Log(response.ScriptData.JSON);
}
});
}
public class InventorySlot
{
public string name = "Item name";
public int quantity = 0;
}
Can anyone tell me how I can send the json string from Unity and have it interpreted the same as it is when entered in the test harness?
Many thanks
Best Answer
C
Customer Support
said
about 6 years ago
Hi Martin,
Our Unity SDK offers a way of wrapping JSON objects into a custom data type to send as attributes called GSRequestData. You can use this to attach keys and values like you would a JSON object.
Our Unity SDK offers a way of wrapping JSON objects into a custom data type to send as attributes called GSRequestData. You can use this to attach keys and values like you would a JSON object.
In your case you can use "AddString(key, value)" for this.
Let me know if this works for you!
-Pádraig
1 person likes this
T
Tetragram Developer
said
almost 4 years ago
Sorry for the late post on this but since I found it, maybe others will too. It's much easier to parse the JSON on the GameSparks side.
I had an JSON, same as Martin, like this:
{"level":1,"credits":0}
With Unity's Parsing it was escaped, but on the GameSparks side I used
var jsonObj = JSON.parse(jsonFromUnity);
jsonObj.level = 3;
And it worked. Seems easier.
1 person likes this
M
Martin Horn
said
about 6 years ago
Hi Pádraig,
thanks, that's exactly what I needed, so I just use reflection to iterate over the fields I want to save from my inventory slot class.
Just in case it's of any use to someone else, this is how I implemented it in my class
public class InventorySlot
{
public string name = "Item name";
public int quantity = 0;
public void SaveToCloud(int index)
{
var fieldValues = this.GetType()
.GetFields()
.ToList();
GSRequestData jsonData= new GSRequestData();
foreach(var field in fieldValues)
{
jsonData.AddString(field.Name, field.GetValue(this).ToString());
}
new GameSparks.Api.Requests.LogEventRequest()
.SetEventKey("SET_SLOT_CONTENTS")
.SetEventAttribute("INDEX", index)
.SetEventAttribute("CONTENTS", jsonData)
.Send(response => {
if(!response.HasErrors)
{
Debug.Log(response.ScriptData.JSON);
}
});
}
}
Martin Horn
Hi,
I'm trying to send a JSON string as an EventAttribute to a cloud event so that I can set multiple field values at once and this is the cloud code I'm using...
In the test harness it works perfectly, if I enter something like this...
I get this as the resulting inventory object...
However if I invoke this from Unity like this...
The resulting inventory object looks like this...
Many thanks
Hi Martin,
Our Unity SDK offers a way of wrapping JSON objects into a custom data type to send as attributes called GSRequestData. You can use this to attach keys and values like you would a JSON object.
For example:
GSRequestData jsonData= new GSRequestData();
jsonData.AddStringList("list_of_strings", string_list);
In your case you can use "AddString(key, value)" for this.
Let me know if this works for you!
-Pádraig
- Oldest First
- Popular
- Newest First
Sorted by PopularCustomer Support
Hi Martin,
Our Unity SDK offers a way of wrapping JSON objects into a custom data type to send as attributes called GSRequestData. You can use this to attach keys and values like you would a JSON object.
For example:
GSRequestData jsonData= new GSRequestData();
jsonData.AddStringList("list_of_strings", string_list);
In your case you can use "AddString(key, value)" for this.
Let me know if this works for you!
-Pádraig
1 person likes this
Tetragram Developer
Sorry for the late post on this but since I found it, maybe others will too. It's much easier to parse the JSON on the GameSparks side.
I had an JSON, same as Martin, like this:
{"level":1,"credits":0}
With Unity's Parsing it was escaped, but on the GameSparks side I used
var jsonObj = JSON.parse(jsonFromUnity);
jsonObj.level = 3;
And it worked. Seems easier.
1 person likes this
Martin Horn
Hi Pádraig,
thanks, that's exactly what I needed, so I just use reflection to iterate over the fields I want to save from my inventory slot class.
Just in case it's of any use to someone else, this is how I implemented it in my class
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics