Player 2 plays the game, gets a score then we check the 2 scores and see who has won.
But i can't seem to get this working with Gamesparks, when i do a CreateChallengeRequest with scriptData of scoreToBeat i don't see it on the challengeInstance?! Is there something i am missing?
Regards
Abdullah
Best Answer
C
Customer Support
said
about 8 years ago
Ah so in this case we must create the challenge, wait for the response, on the response log an event with an attribute for the challengeInstanceId and the score, in the cloud code of that event have it set the scriptData to the challenge like in the example above.
You can do this in Unity by nesting the LogEvent in the Response of the CreateChallengeRequest and taking the challengeInstanceId from the response. The reason we use LogEvent instead of LogChallengeEvent in this case is that if you LogChallengeEvent before the challenge has started/not your turn your event will not fire. LogEvent lets us manipulate the challenge without the safety features getting in the way.
I've added you to a game that has this set up. A run through looks like:
So the challenge technically doesn't exist until the player receives a CreateChallengeResponse, this is where you should set the score to beat. We can do this by taking the challengeInstanceId from the response and using that to load the challenge object and then set the scriptData like so:
var thisChallenge= Spark.getChallenge(Spark.getData().challengeInstanceId);
if(thisChallenge.shortCode === "abdullahsGame"){
challenge.setScriptData("scoreToBeat", 10);
}
As you can see, we are actually checking the shortCode of the challenge to make sure it's the one we want to set scriptData against. This is because CreateChallengeResponse is called for every challenge type and you might not want to store a turn on all of them right away.
Shane
A
Abdullah Awan
said
about 8 years ago
Thanks for the reply, but not quite what i am after.
challenge.setScriptData("scoreToBeat", 10);
the 10 for the value is something i do not want to hardcode as i am setting the ScriptData "scoreToBeat" to X from Unity and the request shows the scriptData going through fine, its when the opponent receives it he has no ScriptData.
So what i am looking for is a way to first getScriptData from the challengeRequest and then setScriptData with that value, so that when my opponent receives the Challenge he can see that the score he needs to beat is X.
Regards
Abdullah Awan
Customer Support
said
about 8 years ago
Answer
Ah so in this case we must create the challenge, wait for the response, on the response log an event with an attribute for the challengeInstanceId and the score, in the cloud code of that event have it set the scriptData to the challenge like in the example above.
You can do this in Unity by nesting the LogEvent in the Response of the CreateChallengeRequest and taking the challengeInstanceId from the response. The reason we use LogEvent instead of LogChallengeEvent in this case is that if you LogChallengeEvent before the challenge has started/not your turn your event will not fire. LogEvent lets us manipulate the challenge without the safety features getting in the way.
I've added you to a game that has this set up. A run through looks like:
Just one more thing I've noticed today. When i do send the LogEvent by Player A and the PlayerB receives the challenge etc. Sometimes when i do a LogChallenge event with PlayerB it says NOT_YOUR_TURN but other times it works fine.
In my cloud code for LogEvent (which is sent by PlayerA straight after the create challenge) i have got the following:
var thisChallenge = Spark.getChallenge(Spark.getData().challengeInstanceId);
It's important to make sure that the challenge is not set to "Turn Based" in the Configurator. While this is a turn based scenario, we know player 1 will always have set their score to the challenge and that once player 2 has received/accepted the challenge it will be there go. This should fix your problem.
Abdullah Awan
I want to set up a very simple challenge where
Player 1 plays the game, gets a score of 10
Sends this score to Player 2
Player 2 plays the game, gets a score then we check the 2 scores and see who has won.
But i can't seem to get this working with Gamesparks, when i do a CreateChallengeRequest with scriptData of scoreToBeat i don't see it on the challengeInstance?! Is there something i am missing?
Regards
Abdullah
Ah so in this case we must create the challenge, wait for the response, on the response log an event with an attribute for the challengeInstanceId and the score, in the cloud code of that event have it set the scriptData to the challenge like in the example above.
You can do this in Unity by nesting the LogEvent in the Response of the CreateChallengeRequest and taking the challengeInstanceId from the response. The reason we use LogEvent instead of LogChallengeEvent in this case is that if you LogChallengeEvent before the challenge has started/not your turn your event will not fire. LogEvent lets us manipulate the challenge without the safety features getting in the way.
I've added you to a game that has this set up. A run through looks like:
Player1:
--------
Player2:
Shane
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
Hi Abdullah,
So the challenge technically doesn't exist until the player receives a CreateChallengeResponse, this is where you should set the score to beat. We can do this by taking the challengeInstanceId from the response and using that to load the challenge object and then set the scriptData like so:
Cloud Code -> Responses -> CreateChallengeResponse
As you can see, we are actually checking the shortCode of the challenge to make sure it's the one we want to set scriptData against. This is because CreateChallengeResponse is called for every challenge type and you might not want to store a turn on all of them right away.
Shane
Abdullah Awan
Thanks for the reply, but not quite what i am after.
challenge.setScriptData("scoreToBeat", 10);
the 10 for the value is something i do not want to hardcode as i am setting the ScriptData "scoreToBeat" to X from Unity and the request shows the scriptData going through fine, its when the opponent receives it he has no ScriptData.
So what i am looking for is a way to first getScriptData from the challengeRequest and then setScriptData with that value, so that when my opponent receives the Challenge he can see that the score he needs to beat is X.
Regards
Abdullah Awan
Customer Support
Ah so in this case we must create the challenge, wait for the response, on the response log an event with an attribute for the challengeInstanceId and the score, in the cloud code of that event have it set the scriptData to the challenge like in the example above.
You can do this in Unity by nesting the LogEvent in the Response of the CreateChallengeRequest and taking the challengeInstanceId from the response. The reason we use LogEvent instead of LogChallengeEvent in this case is that if you LogChallengeEvent before the challenge has started/not your turn your event will not fire. LogEvent lets us manipulate the challenge without the safety features getting in the way.
I've added you to a game that has this set up. A run through looks like:
Player1:
--------
Player2:
Shane
Abdullah Awan
Ah ok Brilliant thats much better! Thanks
Just one more thing I've noticed today. When i do send the LogEvent by Player A and the PlayerB receives the challenge etc. Sometimes when i do a LogChallenge event with PlayerB it says NOT_YOUR_TURN but other times it works fine.
In my cloud code for LogEvent (which is sent by PlayerA straight after the create challenge) i have got the following:
var thisChallenge = Spark.getChallenge(Spark.getData().challengeInstanceId);
thisChallenge.setScriptData("scoreToBeat", Spark.getData().scoreToBeat);
and in the takeTurn ChallengeEvent (which is sent by PlayerB) i have got the following
var thisChallenge = Spark.getChallenge(Spark.getData().challengeInstanceId);
var scoreToBeat = thisChallenge.getScriptData("scoreToBeat");
var playerScore = Spark.getData().playerScore;
var opponentID = Spark.getData().opponentID;
checkWinner();
function checkWinner(){
if(playerScore > scoreToBeat){
thisChallenge.winChallenge(Spark.player);
}
if(playerScore < scoreToBeat){
thisChallenge.winChallenge(Spark.loadPlayer(opponentID));
}
if(playerScore == scoreToBeat){
thisChallenge.drawChallenge();
}
thisChallenge.consumeTurn(Spark.player);
}
Thanks
Customer Support
Hi Abdullah,
It's important to make sure that the challenge is not set to "Turn Based" in the Configurator. While this is a turn based scenario, we know player 1 will always have set their score to the challenge and that once player 2 has received/accepted the challenge it will be there go. This should fix your problem.
Shane
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics