Sign In Register

How can we help you today?

Start a new topic
Answered

Exchanging Data between Players in a Turn-based Challenge

I'm trying to get two players on a turn-based challenge to exchange data between them, 

say player1 calls  :

{
 "@class": ".LogChallengeEventRequest",
 "eventKey": "T_E",
 "challengeInstanceId": "challengeInstanceId",
 "te_v": "123"
}

 with the the cloudcode of the event set to:

Spark.setScriptData("playerid", Spark.getPlayer().getPlayerId());
Spark.setScriptData("playername", Spark.getPlayer().getDisplayName());
Spark.setScriptData("te_v", Spark.getData().te_v);

 

this creates the appropriate .LogChallengeEventRequest but it doesn't save the script data on .ChallengeTurnTakenMessage - here the scriptdata appears as undefined, how can player2 read what has been sent by player1 namely the "te_v" value?


This of course is assuming the right way to get player1 "te_v" value is through the scriptdata in ChallengeTurnTakenMessage. 

If not, do I have to store this value as a collection and then put the value in ChallengeTurnTakenMessage through the cloud code? Is there another way to achieve this?


Thank you.


Best Answer

Hi Pedro,


What I would do in this situation is store the data on the challenge itself.


In the Cloud Code, Spark.setScriptData will only show on the response of the request and won't be stored, it's great for debugging or displaying information to use without using another request. You can save the data to the challenge instance by adding the following to your LogChallengeEvent -> T_E Cloud Code:


   

//Get the challenge instance based on it's Id
var challenge = Spark.getChallenge(Spark.data.challengeInstanceId);


//Set the scriptData of the challenge instance.
challenge.setScriptData("playerid", Spark.getPlayer().getPlayerId());
challenge.setScriptData("playername", Spark.getPlayer().getDisplayName());
challenge.setScriptData("te_v", Spark.getData().te_v);

   

You should now see the values in ScriptData whenever a response or message displays the challenge object.


Shane


Answer

Hi Pedro,


What I would do in this situation is store the data on the challenge itself.


In the Cloud Code, Spark.setScriptData will only show on the response of the request and won't be stored, it's great for debugging or displaying information to use without using another request. You can save the data to the challenge instance by adding the following to your LogChallengeEvent -> T_E Cloud Code:


   

//Get the challenge instance based on it's Id
var challenge = Spark.getChallenge(Spark.data.challengeInstanceId);


//Set the scriptData of the challenge instance.
challenge.setScriptData("playerid", Spark.getPlayer().getPlayerId());
challenge.setScriptData("playername", Spark.getPlayer().getDisplayName());
challenge.setScriptData("te_v", Spark.getData().te_v);

   

You should now see the values in ScriptData whenever a response or message displays the challenge object.


Shane

with the LogChallengeEvent response, it seems like it only returns Spark.setScriptData; not what's set in the challenge.setScriptData.  am I missing something?

Hi Vince,


Yes, you are correct, a LogChallengeEventResponse doesn't return the challenge itself, so scriptData set on the challenge won't appear in your response. There are two ways you can access this data:


1. Call a corresponding Spark.setScriptData() for each of your challenge.setScriptData() calls, which will set both the challenge's and response's script data.


2. Add a GetChallengeRequest to the response function of your LogChallengeEventRequest. This will return the challenge instance, from which you can obtain any of the scriptData fields required.


If you have any more questions please let us know.


Regards,

Vinnie

Login to post a comment