Sign In Register

How can we help you today?

Start a new topic

createChallengeRequest through CloudCode

So, I've got CreateChallengeRequest running through Unity setting EndTime through system.Date but I realise users can change their machine clock and then it will mess up with my ChallengeInstances.

So I tought that a better way to do it was to create an Event and call it in Unity with LogEventRequest.
Is this right? Or do I have another way to do it?

Here is what I got


var endDate = new Date(*60*60*24));
var expiryDate = new Date(*60*10));
var userIds =;

var request = new SparkRequests.CreateChallengeRequest();

    request.challengeShortCode = "newChallenge" ;
    request.usersToChallenge = userIds ;
    request.endTime = endDate ;
    request.expiryTime = expiryDate ;
    request.challengeMessage = "New Challenge Started!" ;

 However, when I call this through TestHarness with a valid userId, no Issued Challenge has been created. And TestHarness doesn't return any errors .


  "@class": ".LogEventResponse"


I got the same with 



and my script.log is empty

Hi Flávio,

This would definitely be a valid way to create your challenges. Are your challenged players receiving ChallengeIssuedMessages? You won't see anything in the LogEventResponse because the responses to requests sent within your event won't be added to the event's response automatically.

It would definitely be worth adding your CreateChallengeResponse to the response for the event to confirm that the challenge is being created properly:

var response = request.Send();

Spark.setScriptData("CreateChallengeResponse", response);

Try this out and let us know what you see.



Thank you for your response and for the setScriptData to response tip. Silly me I didn't though of it .

I was about to open a topic how to parse dates correcly but then I thought I've found a right way to do it.
Well... seems not :)


  "@class": ".LogEventResponse",
  "scriptData": {
    "CreateChallengeResponse": {
      "@class": "GameSparksErrorResponse",
      "errors": {
        "message": "java.text.ParseException: Unparseable date: \"{\" (through reference chain: com.gamesparks.messages.requests.CreateChallengeRequest[\"endTime\"])"


I found the topic that support helped a user parse time correctly and I can set time with


var battleEndDate = new Date(*60*60*24));
var newBattleEndDate = battleEndDate.getFullYear()+ "-" + battleEndDate.getUTCMonth() + 1 +"-"+ battleEndDate.getUTCDate() + "T" + battleEndDate.getUTCHours() +":" + battleEndDate.getUTCMinutes()+ "Z";
var battleExpiryDate = new Date(*60));
var newBattleExpiryDate = battleExpiryDate.getFullYear()+ "-" + battleExpiryDate.getUTCMonth() + 1 +"-"+ battleExpiryDate.getUTCDate() + "T" + battleExpiryDate.getUTCHours() +":" + battleExpiryDate.getUTCMinutes()+ "Z";

 However my users will be notified through push notification that they have for ex 1 or 2minutes to login and complete the challenge , does GameSparks have a Seconds Tag ? 

Hi Flavio,

No, for setting challenges the times are set to the closest minute. You can't use seconds for challenge times I'm afraid.



Here is a workaround for people that have the same need


var battleEndDate = new Date(*60*60*24));
var month = battleEndDate.getMonth() + 1;
var hour = battleEndDate.getHours() ;
var newBattleEndDate = battleEndDate.getFullYear()+ "-" + month +"-"+ battleEndDate.getUTCDate() + "T" + hour +":" + battleEndDate.getUTCMinutes()+ "Z";

var userIds =;

var thisrequest = new SparkRequests.CreateChallengeRequest();

    thisrequest.challengeShortCode = "newChallenge" ;
    thisrequest.usersToChallenge = userIds ;
    thisrequest.endTime = newBattleEndDate ;
    thisrequest.challengeMessage = "You got a new Challenge!" ;
    var response = thisrequest.Send();
    var instanceId = response.challengeInstanceId;
var acceptRequest = new SparkRequests.AcceptChallengeRequest();

  acceptRequest.challengeInstanceId = instanceId;
var theScheduler = Spark.getScheduler();
theScheduler.inSeconds("TIME_TO_EXPIRE", 60, {"challengeInstanceId" : instanceId});


Then on TIME_TO_EXPIRE Module :


var currentChallengeId = Spark.getData().challengeInstanceId;

Spark.getChallenge(currentChallengeId).setScriptData("Expired", 1);


I have user DrawChallenge() and do my WinCondition functions on ChallengeDrawnMessage because my Project doesn't have draws.

In my case this Works because my challenges are super quick, but we could use Expired as we wish. :)

Login to post a comment