Sign In Register

How can we help you today?

Start a new topic
Answered

Reading contents of message sent by cloudcode (Unity C#)

All I want to do is read the message sent through cloud code.


Here is the cloud code


 

var observerDataList = Spark.runtimeCollection("observerData");
var displayNameToFind = Spark.getData().DISPLAYNAME; // The player's display name I am searching for
var observerData = observerDataList.findOne({"displayName": displayNameToFind});
Spark.sendMessageById({"SendData" : "1"}, observerData.playerID)

 

and C#


 

        GameSparks.Api.GSMessageHandler._AllMessages = ((GSMessage e) => {

            object value;
            DebugText.instance.Log("ALL HANDLER " + e.MessageId);
            value = e.JSONData.TryGetValue("DataSent", out value);
            Debug.Log(value);
             });

 

I was kind of guessing in the dark here though.


How do I get the data out of the message?


Thanks


Jim


Best Answer
Hi Jim!

For sending messages + data i would recomend the following approach:

First you will need to create a new ScriptMessage in Configurator>Messages>ScriptMessage extensions, this will be the message that will carry your data.


Then you will need to create an Event that you can call to act as a messenger. The Attributes of this event, will be the data variables you want to send:

bove in my example i have a String and json attribute to test with.


In the Cloud code for the messenger Event i add the following-


var message = Spark.message("DATA_MSG");//The short code of your script message.

message.setMessageData({"sendTo":Spark.getData().SEND_TO_PID,"data": Spark.getData().DATA_TO_SEND });//Add Data to the events attributes - note the Attributes Short codes being used to identify your data.

message.setPlayerIds(Spark.getData().SEND_TO_PID);//send the message to a player id - in my case, the SEND_TO_PID attribute
message.send();


This is all the cloud code required-


Now to Unity-

You will nowneed to updated your customSDK from GameSparks>Settings>Get my Custom SDK, so were can get a reference to the message you just created.


You will then need to set up a message delegate to listen for this custom message, much like you would listen for one of Gamesparks Messages.


  GameSparks.Api.Messages.ScriptMessage_DATA_MSG.Listener += Data_MessageHandler; //Listener


  void Data_MessageHandler(GameSparks.Api.Messages.ScriptMessage_DATA_MSG _message)//Delegate method
  {
  Debug.LogWarning("DATA_MSG recieved");

  string sendTo = _message.Data.GetString("sendTo");// GetString/GetNumber - simple variables

  Debug.LogError(sendTo);
  Debug.Log( _message.Data.JSON);  //Json object for more complicated data packages.
  }


In order to send Data from within unity simply use LogEventRequest, and fill in your data.


Hope this helps,


Regards,

-Dave



 

1 Comment

Answer
Hi Jim!

For sending messages + data i would recomend the following approach:

First you will need to create a new ScriptMessage in Configurator>Messages>ScriptMessage extensions, this will be the message that will carry your data.


Then you will need to create an Event that you can call to act as a messenger. The Attributes of this event, will be the data variables you want to send:

bove in my example i have a String and json attribute to test with.


In the Cloud code for the messenger Event i add the following-


var message = Spark.message("DATA_MSG");//The short code of your script message.

message.setMessageData({"sendTo":Spark.getData().SEND_TO_PID,"data": Spark.getData().DATA_TO_SEND });//Add Data to the events attributes - note the Attributes Short codes being used to identify your data.

message.setPlayerIds(Spark.getData().SEND_TO_PID);//send the message to a player id - in my case, the SEND_TO_PID attribute
message.send();


This is all the cloud code required-


Now to Unity-

You will nowneed to updated your customSDK from GameSparks>Settings>Get my Custom SDK, so were can get a reference to the message you just created.


You will then need to set up a message delegate to listen for this custom message, much like you would listen for one of Gamesparks Messages.


  GameSparks.Api.Messages.ScriptMessage_DATA_MSG.Listener += Data_MessageHandler; //Listener


  void Data_MessageHandler(GameSparks.Api.Messages.ScriptMessage_DATA_MSG _message)//Delegate method
  {
  Debug.LogWarning("DATA_MSG recieved");

  string sendTo = _message.Data.GetString("sendTo");// GetString/GetNumber - simple variables

  Debug.LogError(sendTo);
  Debug.Log( _message.Data.JSON);  //Json object for more complicated data packages.
  }


In order to send Data from within unity simply use LogEventRequest, and fill in your data.


Hope this helps,


Regards,

-Dave



 

Login to post a comment