Sign In Register

How can we help you today?

Start a new topic
Answered

Adding Custom Data to ChallengeStartedMessage

I am trying to pass some custom data in ChallengeStartedMessage to players. Here in the sample I am trying to set "x" to "abc"


Following is my cloud code for ChallengeStartedMessage

require("Board");
var challengeID = Spark.getData().challengeInstanceId;
var challenge = Spark.getChallenge(challengeID);
var board = createBoard(challengeID);

data = {};
data["x"] = "abc";
Spark.setScriptData(data);

 But the scriptData of ChallengeStartedMessage when viewed in TestHarness Inspector is an empty json object.


Can you please let me know what I am doing wrong here?


Best Answer

Hi Sudhindra,


I can see a few things that would be preventing you from setting your custom data on your ChallengeStartedMessage.


Firstly, in your Cloud Code for your ChallengeStartedMessage, on line 2, you are using Spark.getData().challengeInstanceId but in the ChallengeStartedMessage, this key you are looking for is challengeId. You were receiving a fatal error here which would halt your script at that line and just display a normal ChallengeStartedMessage. If you change line 2 to the following, your code won't error out:


var challengeID = Spark.getData().challengeId;


Also, when setting script data, you would want to be using the format Spark.setScriptData(string name, JSON value) rather than constructing the JSON and then passing it in as 1 value. Keep in mind the value can be a simple value or a JSON value. You can read more about this in our documentation here.


I hope this answers your question!


Regards,

Cillian.


 Following is my cloud code for MatchFoundMessage

data = {};
data["x"] = "abc";
Spark.setScriptData("mytest", data);
if (Spark.getPlayer().getPlayerId() === Spark.getData().participants[0].id) {
    var tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1);
    var request = new SparkRequests.CreateChallengeRequest();
    request.challengeShortCode = "DefaultChallenge";
    request.endTime = tomorrow.toISOString();
    request.usersToChallenge = []
    request.usersToChallenge.push(Spark.getData().participants[1].id);
    request.usersToChallenge.push(Spark.getData().participants[2].id);
    request.usersToChallenge.push(Spark.getData().participants[3].id);
    request.Send();
    
}

In this case the Script data comes correctly as a part of the message when viewed on the TestHarness Inspector.  What could be the reason why it does not work on ChallengeStartedMessage when it works fine for MatchFoundMessage?

Answer

Hi Sudhindra,


I can see a few things that would be preventing you from setting your custom data on your ChallengeStartedMessage.


Firstly, in your Cloud Code for your ChallengeStartedMessage, on line 2, you are using Spark.getData().challengeInstanceId but in the ChallengeStartedMessage, this key you are looking for is challengeId. You were receiving a fatal error here which would halt your script at that line and just display a normal ChallengeStartedMessage. If you change line 2 to the following, your code won't error out:


var challengeID = Spark.getData().challengeId;


Also, when setting script data, you would want to be using the format Spark.setScriptData(string name, JSON value) rather than constructing the JSON and then passing it in as 1 value. Keep in mind the value can be a simple value or a JSON value. You can read more about this in our documentation here.


I hope this answers your question!


Regards,

Cillian.


1 person likes this

Hi Cillian,


I have incorporated the changes that you have mentioned and it did solve my original question. Thank you.


Since you mentioned that I was receiving a fatal error, can you let me know how I can check for such errors in TestHarness? It did not show me any errors.


I could also have some other errors in my board module, which would lead to the behavior that you mentioned. Hence I want to know how to debug in such cases. 


Regards,

Sudhindra Bhat


I found the errors in Game - Data Explorer - Runtime - script.log.


Is this the correct place to look for errors? Or Should I check in some other places as well?

Hi Sudhindra,


Glad to hear that the above worked for you! 


The script.log is the correct place to look for errors and debug messages. The easiest way to filter FATAL error logs is to enter {"level":"FATAL"} into the query field when searching. 


I hope this answers your question!


Regards,

Cillian.



Hi,


Also to better debug cloud code modules, is there a way to write into debug logs? If so could you point me to some documentation on how to do it?


Regards,

Sudhindra Bhat

found the log module. That concludes this.

Login to post a comment