Sign In Register

How can we help you today?

Start a new topic
Answered

Challenge script data reliability

Hi,


I am occasionally experiencing unreliability with some data being set to challenge's scriptData. I am setting the data in CloudCode ChallengeJoinedMessage but when the message is received on the clients (Test Harness or mobile app) the data is simply not there. After that, once user takes turn, the ChallengeTurnTakenMessage has this data (and it is not being set again).


Also, I noticed in CloudCode I cannot query the Challenge object to receive the next player's ID (getNextPlayer() method is missing, client SDKs do have this method). Therefore I am setting (in ChallengeJoinedMessage) nextPlayer value to the challenge's scriptData to be able to receive it in other scripts but querying Spark.getData().challenge.nextPlayer (sometimes) returns undefined, yet the ChallengeJoinedMessage received by the clients does contain the nextPlayer in its challenge node.


So as of now, I am quite worried what may happen when the game goes live and not quite sure how to handle this 'unreliability'.


Would appreciate any tips on how to possibly avoid such issues.


Regards,


Marcel


Best Answer
Hey Marcel,

[1] - Yes, i think in your case you should do your setup when the challenge is created, in CreateChallengeResponse. If you need to modify it after that then you could use a separate LogEventRequest before calling the TakeTurn or ChallengeJoined for example.
[2] - you can defiantly create the challenge in the response of the FindMatchRequest. Just get one of the playerIds and challenge the other playerId. The response script should still get called, and you can assign the setup-data there.
[3] - If you need to get the nexPlayerData after the CreateChallengeRequest, you could send a LogEventRequest immediately after the CreateChallengeRequest in the client, the challenge should be setup by that stage and there will be no problem.

Once your challenge is setup, you can get/set the challenge script data using..

var scriptData = Spark.getChallenge(Spark.data.challengeId).getScriptData("data");
Spark.getChallenge(Spark.data.challengeId).setScriptData("data", newStuff);

That example will only work in the challenge responses, but if you know the challenge ID, you can get that challenges details from anywhere just using Spark.getChallenge (so long as it has started of course).

Does that clear things up?
-Sean

 


Hey Marcel,

Im a little confused as to what you are trying to do, so if i get it straight, you are trying to send some script data attached to your challenge? And you are using a turn-based challenge, as opposed to an asynchronous challenge?

Looking at your current game i can see that you are starting the challenge in the ChallengeJoined global message script. I say for sure that you dont need to do that there, as the challenge will automatically be started as soon as a second player joins; no need for call startChallenge.

Also, when you send the ChallengeJoined message, the cloud code will only run after the message is sent, so the players will not get the new script-data. But you will have added the data after the messages were sent, so when you receive the TurnTaken message you will see the updated message.
So what you need is to add the script-data when the challenge is created. Either that or you could have a LogEventRequest where you send the data you want to update the challenge's script-data with info, and then call that request before you join the challenge (or in the order: update script data, then join challenge).

As for why next player is returning undefined, without knowing more about what you are doing there, i can tell you that the only reason this would be undefined is if the challenge has not yet started when you are asking for the next player. Either that or there could be some error occurring before the call.

I hope that helps. If you can update me with some details on what you want to do and if i could get a clearer picture on how the nextPlayer problems are happening im sure we'll figure it out.

-Sean

 

Hi Sean,


Thank you for response.


I will try to be more clear about what I am trying to do. Also, I think you were looking at my setup GameSparksDemo, it is not the one I was talking about - I am currently working on the other game in my account.


So, I am using turn-based challenges which are always 1v1 and these are created in a way to start automatically when maxPlayers are joined - I am not calling startChallenge in ChallengeJoinedMessage in this other setup. What I want to do is to generate (by GameSparks) some initial game data for the players (this data is a set of random and constant values) - that is what I want to set in the challenge's script data.


So what you need is to add the script-data when the challenge is created. Either that or you could have a LogEventRequest where you send the data you want to update the challenge's script-data with info, and then call that request before you join the challenge (or in the order: update script data, then join challenge).

In order to set the initial game data, should I use CloudCode's CreateChallengeResponse? This seems more straightforward than sending a separate LogEventRequest.

Related question: I am currently sending the CreateChallengeRequest by one of the players (so the CreateChallengeResponse's CloudCode will be executed). But, I am eventually planing to use the FindMatchRequest and its corresponding response. In the FindMatchResponse's CloudCode I would ideally want to create a challenge between the matched players. Will CreateChallengeResponse's CloudCode be executed for a challenge created in this way? Or should I send the CreateChallengeRequest by one of the players?


NextPlayer returning undefined:

undefined was returned when executing the following in CloudCode of ChallengeJoinedMessage:

Spark.getData().challenge.nextPlayer

So perhaps, in some cases, this is too early to ask for the next player.

A little info on why I want the next player's ID in the challenge's script data: I want each turn to be time limited. So, in order for the players to be at least a little synchronized, I am asking them to log an event which says they are ready for the next turn (does not matter who takes the turn). Once both players are ready, I send them a message that the turn starts and create a scheduler with an ID of "SOME_CONST + {nextPlayerID}". Now, either the next player takes turn before the scheduled task is executed (therefore I cancel the scheduler using its ID), or the scheduled task is executed and the turn is taken automatically. Obviously, it would be useful if a Challenge object had getNextPlayerID() but since it is not available I decided to store it in the challenge's script data. I am not sure which CloudCode message/response I should use to access this variable so that I can reliably set it before each turn (or is there some other unique ID I could use instead?).


Hopefully that gives you a better idea of my intentions.


Thanks for your time,


Marcel

Answer
Hey Marcel,

[1] - Yes, i think in your case you should do your setup when the challenge is created, in CreateChallengeResponse. If you need to modify it after that then you could use a separate LogEventRequest before calling the TakeTurn or ChallengeJoined for example.
[2] - you can defiantly create the challenge in the response of the FindMatchRequest. Just get one of the playerIds and challenge the other playerId. The response script should still get called, and you can assign the setup-data there.
[3] - If you need to get the nexPlayerData after the CreateChallengeRequest, you could send a LogEventRequest immediately after the CreateChallengeRequest in the client, the challenge should be setup by that stage and there will be no problem.

Once your challenge is setup, you can get/set the challenge script data using..

var scriptData = Spark.getChallenge(Spark.data.challengeId).getScriptData("data");
Spark.getChallenge(Spark.data.challengeId).setScriptData("data", newStuff);

That example will only work in the challenge responses, but if you know the challenge ID, you can get that challenges details from anywhere just using Spark.getChallenge (so long as it has started of course).

Does that clear things up?
-Sean

 

Yes, your replies are really helpful. Thank you, Sean.


Marcel

Login to post a comment