Sign In Register

How can we help you today?

Start a new topic

Join Random Challenge

It would be great if JoinChallengeRequest challengeInstanceId parameter would be not required and in case if it's not set the request would search for the random challenge. In case if SetEligibility is used it would join a random challenge that matches eligibility criteria.

1 person likes this idea

Hi Anatolli,

What are you trying to achieve here?
Create a random challenge?



Hi Oisin.

I would say not to create a random challenge but to join one of available challenges in case if there are any.


This feature is set to Implemented. But ChallengeInstanceId is still required in JoinChallengeRequest. If it's implemneted how can I use it?


Hi Anatolli,

I'm not sure how this was marked as implemented as it currently isn't, there is a way to do this in Cloud Code. When a player performs a FindChallengeRequest to see what games are available to join, normally they would get list of the open challenges back in the response. What we can do is grab the challengeIds in the response and pick one from random and use a SparkRequest to Join the challenge. I've included some code below which you can add to your FindChallengeResponse.


//get the array of challengeIntances from the response

var challenges = Spark.getData().challengeInstances;

//an array to parse our challengeId's into
var challengeInstanceId = []; 

//for every object in the challenges array, get the challengeId field and push to challengeInstanceId[]
for(instanceId in challenges )
//generate pseudorandom number in our defined range. 
var randNum = Math.floor((Math.random() * challengeInstanceId.length) + 0); 
//reference the id at that random numbers location
var randomChallengeId = challengeInstanceId[randNum];
//each time you run this code, a different id is set in the scriptdata
Spark.setScriptData("challenge to join", randomChallengeId);

//now we have our random challenge we can join it with a SparkRequest
var request = new SparkRequests.JoinChallengeRequest();

//join our random challenge
request.challengeInstanceId = randomChallengeId;

var response = request.Send();


This is essentially turning your FindChallengeRequest into a FindAndJoinRandomChallenge custom event. Hope this helps, if you have any other questions just let me know.



Login to post a comment