Sign In Register

How can we help you today?

Start a new topic
Answered

ScriptMessage & Unity

Hi,


I am trying to get a very simple example working. I want every minute to send a message to all the players.

So I modified the cloude code of the system event GS_MINUTE with this:

 

var players = Spark.getPlayerIds();
Spark.sendMessageById({"TEST" : "DTC"}, players);

 Then in unity I have an object with the following script attached:

 

public class TEST : MonoBehaviour 
{
    void Awake() 
    {
        ScriptMessage.Listener = (ScriptMessage message) =>
        {
            Debug.Log("We just got a ScriptMessage");
        };
 
        GSMessageHandler._AllMessages = (GSMessage e) =>
        {
            Debug.Log("ALL HANDLER " + e.MessageId);
        };
    }
}

 

But I do not get any printout in the console. I also tried to put the cloud code in an event and to trigger it from the testharness, but without more success.


Best Answer

Hi Stephanie,


Good catch. Yes, the getPlayerIds() method is specific for the Global Message Cloud Code. Though you're use of it is correct, it will not work in this context because EVERY_MINUTE will not have Player context whereas Global Message has global Player context.


What you'll need is a context of the player's external to it - the following post should prove useful in setting this up. https://support.gamesparks.net/discussions/topics/1000070846


Also, what exactly were you planning on sending every minute? We strongly recommend you avoid sending messages in the EVERY_MINUTE script.


Thanks,

Vinnie


Hi Stephane,

Can you paste the inspector log from the test harness please.
Might help us narrow down the cause.


Thanks,


Oisin

I don't think that it will be very informative:

{
 "@class": ".LogEventResponse",
 "scriptData": null
}
{
 "@class": ".LogEventRequest",
 "eventKey": "TEST_EVENT"
}

{
 "@class": ".AuthenticationResponse",
 "authToken": "87aa662f-ba31-4c1f-9ad1-fed15c612397",
 "newPlayer": true,
 "scriptData": null,
 "userId": "56c1e3a8f528a1ccf49b385a"
}

Up.

I still have this issue. I managed to send messages to the users in other pieces of cloud code, but I do not seem to be able to do so in the EVERY_MINUTE and EVERY_DAY codes. Well, at least nothing is received in Unity. I'm not sure if there is a way to check if something was fired from GS server.

That is probably not an error in the script, as I was mentioning the test harness do not cry and the code is trivial:

 

var players = Spark.getPlayerIds();
Spark.sendMessageById({"TEST" : "DTC"}, players);

 


Hey Stephane,

Could you try....

GameSparks.Api.Messages.ScriptMessage.Listener = (message) =>

{

     Debug.Log("We just got a ScriptMessage");

};


Please let us know if that works.

Thanks,
Sean

Does not seem to change anything.

I noticed the cloud code I used is now marked as deprecated. I changed it to this, 

var players = Spark.getPlayerIds();
var msg = Spark.message(null);
msg.setPlayerIds(players);
msg.setMessageData({"type" : "MinuteUpdate", "Message":"This is a minute update."});
msg.send();

 

but it is still the same: I do not get anything on Unity's side, neither in the editor nor deployed on a device.

Actually I think I found what is the issue here, I added this line to my cloud code to be able to inspect the content of the variables:

 

var players = Spark.getPlayerIds();
var msg = Spark.message(null);
msg.setPlayerIds(players);
msg.setMessageData({"type" : "MinuteUpdate", "Message":"This is a minute update."});
msg.send();

Spark.setScriptData("Debug.PlayersIDs", players)

 It turns ou in the inspector that the response is the following (the player list is empty.):

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "Debug.PlayersIDs": []
 }
}

Answer

Hi Stephanie,


Good catch. Yes, the getPlayerIds() method is specific for the Global Message Cloud Code. Though you're use of it is correct, it will not work in this context because EVERY_MINUTE will not have Player context whereas Global Message has global Player context.


What you'll need is a context of the player's external to it - the following post should prove useful in setting this up. https://support.gamesparks.net/discussions/topics/1000070846


Also, what exactly were you planning on sending every minute? We strongly recommend you avoid sending messages in the EVERY_MINUTE script.


Thanks,

Vinnie

Hi Vinnie,


Thanks for the pointer. Indeed I managed to have it working with maintaining my own collection.


Actually I'm going to have this script executed every hour. 

I am just putting it in every minute to make it easier to debug. The idea is to update some decaying values that are stored against each player, and warn them when they are too low.


Thank you again,

Stephane

Hi Stephane,


Glad that it's working for you.


That's fine, there shouldn't be an issue with running that hourly. Please contact us if you need any further assistance.


Vinnie

Hello,


I have a very similar setup but am still having a hard time figuring out the player id array to send to all players. I have a runtime collection storing player names and ids (playerCollection) but I can't figure out how to extract just the ID part of the documents to set the player ID array. Can anyone help?

Login to post a comment