Sign In Register

How can we help you today?

Start a new topic
Answered

Sending and receiving simple message in Unity

Hello


What would be the easiest way to send and receive a message to multiple players from the Unity SDK?


I would like to send the mouse position every second, i see the following options:


1)

  • Create a custom event
  • Send the event request from Unity
  • In cloud code, use Spark.sendMessage to send a message to the correct players


2)

  • I see there's new SparkMessage() in the Unity API? Does that mean, i could send a message directly to players without creating custom events?


Since i could not find a way to use new SparkMessage() directly, I'm assuming that #1 is the only way to achieve this, is that correct?

If yes, could i prevent the response from being sent back to the Player?


Other question, i see that there is a LogEventRequest an LogChallengeEventRequest. In which situation would i want to use LogChallengeEventRequest? I don't quite see the use case.


Many thanks


Best Answer

Hi Thomas,


Sending mouse coordinates through the messages system might not be the quickest way to go about this, I'd recommend Unity's built in networking or something like Tasharen Networking to accomplish this with minimal lag.


However, if you wish to give it a try number 1 is the way to go about it.



You'd probably be using a custom ScriptMessage and in this case I'd make a custom Extension of "MouseCursorMoved" or something along those lines. Then in Cloud Code use something like:


   

var mousePos = Spark.getData().mousePos;
//Spark.getData().mousePos would be an attribute you set on the custom event

 Spark.sendMessageExt({"mousePos" : "Spark.getData().mousePos"},
    "MouseCursorMoved", 
    [arrayOfPlayersToReceiveMessage]);

   


 

using GameSparks.Api.Messages;
  
public class MessageListen : MonoBehaviour
{
  
        public void Start()
        {
                GameSparks.Api.Messages.ScriptMessage.Listener += GetMessages;
        }
  
        public void GetMessages(ScriptMessage message)
        {
                if (message.ExtCode == "FirstScriptMessage")
                {
                        //Do some stuff
                }
  
                if (message.ExtCode == "SecondScriptMessage")
                {
                        //Do some other stuff
                }
        }
}

 

In regards to the LogEvent/LogChallengeEvent Request, the difference is that LogChallengeEventRequest has a prebuilt attribute for challengeInstanceId that is always available. This helps save some time and frustration if/when a user forgets to include it in their event (as I occasionally do!)


Shane


Answer

Hi Thomas,


Sending mouse coordinates through the messages system might not be the quickest way to go about this, I'd recommend Unity's built in networking or something like Tasharen Networking to accomplish this with minimal lag.


However, if you wish to give it a try number 1 is the way to go about it.



You'd probably be using a custom ScriptMessage and in this case I'd make a custom Extension of "MouseCursorMoved" or something along those lines. Then in Cloud Code use something like:


   

var mousePos = Spark.getData().mousePos;
//Spark.getData().mousePos would be an attribute you set on the custom event

 Spark.sendMessageExt({"mousePos" : "Spark.getData().mousePos"},
    "MouseCursorMoved", 
    [arrayOfPlayersToReceiveMessage]);

   


 

using GameSparks.Api.Messages;
  
public class MessageListen : MonoBehaviour
{
  
        public void Start()
        {
                GameSparks.Api.Messages.ScriptMessage.Listener += GetMessages;
        }
  
        public void GetMessages(ScriptMessage message)
        {
                if (message.ExtCode == "FirstScriptMessage")
                {
                        //Do some stuff
                }
  
                if (message.ExtCode == "SecondScriptMessage")
                {
                        //Do some other stuff
                }
        }
}

 

In regards to the LogEvent/LogChallengeEvent Request, the difference is that LogChallengeEventRequest has a prebuilt attribute for challengeInstanceId that is always available. This helps save some time and frustration if/when a user forgets to include it in their event (as I occasionally do!)


Shane

Great, many thanks for the detailed reply and example code.

 

Hi,

   I want to send message to all my online players. I see that "sendMessageExt" is now deprecated. I try to use SparkMessage, but I don't know how to listen to SparkMessage


Thanks

Hello Team Savie


Here is the modified code from Shane's code:


  

public void Start()
	{
		GameSparks.Api.Messages.ScriptMessage.Listener += GetMessages;
	}

	public void GetMessages(GameSparks.Api.Messages.ScriptMessage message)
	{
//Build_Completed is the shortcode for Script Message
		if (message.ExtCode == "Build_Completed")
		{
			Debug.Log ("Got message " + message.Title);
		}

		if (message.ExtCode == "SecondScriptMessage")
		{
			//Do some other stuff
		}
	}

 Regards. 

Login to post a comment