Sign In Register

How can we help you today?

Start a new topic
Answered

Send JSON string to LogEventRequest

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...


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...

{
 "@class": ".LogEventRequest",
 "eventKey": "SET_SLOT_CONTENTS",
 "CONTENTS": {"name":"Wood","quanitiy":275},
 "INDEX": 0
}

 I get this as the resulting inventory object...

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "data": [
   {
    "_id": "5877a4800a162ad63e82b0ba",
    "SLOT_0": {
     "name": "Wood",
     "quanitiy": 275
    }
   }
  ]
 }
}

 However if I invoke this from Unity 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;
	}

 

The resulting inventory object looks like this...

 

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "data": [
   {
    "_id": "5876509c86972904bc4f2b64",
    "SLOT_1": "{\"name\":\"Wood\",\"quantity\":275}"
   }
  ]
 }
}

 

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

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


Answer

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

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);
			}
		});
	}
}

 

Thanks again

Login to post a comment