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
C
Customer Support
said
about 8 years ago
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!)
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
T
Thomas Zweifel
said
almost 8 years ago
Great, many thanks for the detailed reply and example code.
T
Team Savie
said
almost 7 years ago
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
S
Saad Anees
said
over 6 years ago
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
}
}
Thomas Zweifel
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)
2)
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
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:
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
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
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:
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
Thomas Zweifel
Team Savie
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
Saad Anees
Hello Team Savie
Here is the modified code from Shane's code:
Regards.
-
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