Sign In Register

How can we help you today?

Start a new topic

How to properly take a turn?

Hi guys,

Following the tutorials I created a Cloud Code event to allow players to take turn. But, for every player that takes a turn, I dont see the  "turn" data being increased:

 "turnCount": {
   "5805443a4d6a8d056d1e1264": 0,
   "58054401b6a70d056d0957e5": 0

This is always the response I get when taking turns.... What am I doing wrong?

Essentially I need to increate the turn count every time the game has a full round of turns


2 people have this question

Hi Felipe, 

Unfortunately this feature doesn't work. I'll bring it up with the devs and pursue a solution.

For now what you can do, is create your own "turnCount" object with your players' ID as key, save it in the Challenge's scriptData and keep track of their turns through either ChallengeTurnTaken Message or a Challenge Log Event. I could help you set it up if you'd like.



Hi Omar,

Thanks for replying back to me. I would love if you could help me out with this.

Honestly I would like to control this in the Cloud Code, is this possible? I'd like to make the implementation for the clients simple(er).

I was thinking that the best place to do this would be in the ChallengeTurnTakenMessage - what do you think? I am hopping I can access the challenge data there like this:

var chal = Spark.getChallenge(Spark.getData().challenge.challengeId);

So, if I can I can try to retrieve the turnCount integer. If it doesnt exist, create it with value 1.

Now, I only want to increment it when Player1 and Player2 have taken each their turns for turn 1. Then it goes to turn 2. Is there any way I can do that?

And, when I reach turn 5 and both have played it, I can then compute which player own and send a winChallenge() message to the winner.


Ok!! This was easier than I thought it would be. I got this woring by controlling in the Cloud Code.

So, my Log Challenge Event Take Turn looks like this:

//Load challenge

var chall = Spark.getChallenge(Spark.getData().challengeInstanceId);

//Retrieve player Id

var pId = Spark.getPlayer().getPlayerId();

//Retrieve player's points

var turnPoints = Spark.getData().points;

//Retrieve player's turn

//var round = Spark.getData().round;

//Setting the total points to the player. This will be used in ChallengeTurnTakenMessage to decide who won on turn 5

var playerCurrentPoints = Spark.getPlayer().getScriptData("points") === undefined ? 0 : Spark.getPlayer().getScriptData("points");

var newPoints = playerCurrentPoints + turnPoints;

Spark.getPlayer().setScriptData("points", newPoints);

var currentTurn = chall.getPrivateData("turn");

if (currentTurn === undefined) {

 chall.setPrivateData("turn", 1);

} else {

    currentTurn = currentTurn + 1;

    chall.setPrivateData("turn", currentTurn);


//Finish player turn


Then, my UserMessage ChallengeTurnTakenMessage looks like this

var chal = Spark.getChallenge(Spark.getData().challenge.challengeId);

var turn = chal.getPrivateData("turn");

if(turn == 4) {


    var challengerPlayer = Spark.loadPlayer(chal.getChallengerId());

    var challengedPlayer = Spark.loadPlayer(chal.getChallengedPlayerIds()[0]);

    var challengerPoints = challengerPlayer.getScriptData("points");

    var challengedPoints = challengedPlayer.getScriptData("points");


    //If challenged reaches 0 or lower health while challenged has more than 0 health, challenged wins

   if(challengedPoints > challengerPoints){


    } else {

        //If challenger reaches 0 or lower health whole challenger has more than 0 health, challenger wins




So each player increments the turn by 1. So if my game has for example 2 rounds, the turn count will be 4 (because my games only takes 2 player).

When I reach 4 turns, I then calculate the player with most points (the points are calculate in the clients and sent in the TakeTurn event) and send the Win message!

Now I just need to get this working on my Android game!! :)

Hi, wondering if this has been fixed yet?

I don't think this is fixed, I'm experiencing the same thing. I use challenge.takeTurn(playerId) and a ChallengeTurnTakenMessage goes out but nextPlayer in that message is always the same as the person that's taking the turn. The turn counters never change either.

 It's still not functioning. Sorry guys. You'll still have to have your own solutions, which I recommend saving in the Challenge's scriptData.

Sorry for any inconveniences,


Login to post a comment