So in order to get the script-data from that request you will need to use Spark.getData().getScriptData(), and then your attribute (i.e. Spark.getData().getScriptData().attribute).
So you need to access the data coming from the request and that data contains the script data.
There isnt any difference between Spark.data and Spark.getData() Spark.data was an old call we dont use anymore, but it is still possible to use it.
Hope that helps, Sean
K
Kristaps Auzins
said
about 5 years ago
Right now tryed this stuff and it does not work :(
Made function in cloud code: Spark.getData().getScriptData().param1
But when I run it in Test Harness, I get error:
Function getScriptData does not exist.:
How so???
In JSON data is set: "scriptData":{"param1":"1"}
K
Kristaps Auzins
said
about 5 years ago
Answer
But guess what, it works if you use code like this: Spark.getData().scriptData.param1
Customer Support
said
about 5 years ago
Hey Kristaps,
Thats my bad, sorry. getScriptData() will only work for a few objects like the player.
In other cases it will just appear as an attribute of getData().
That was an oversight on my part.
Does this work for you now?
Sean
R
Rich Joslin
said
about 5 years ago
Kristaps' answer worked for me. Thank you both!
J
James Gregory
said
over 4 years ago
I have a similar problem, when i send over scriptdata from unity, it sends everything fine. However when i try to check if it contains anything in the authentication request cloud code, it never does.
If i use the test harness and send over a "passwordRecoveryRequest" action the code gets used appropriately. Any help?
Unity Code authentication:
public void ForgottonPassword()
{
//Create a data object
GameSparks.Core.GSRequestData scriptData = new GameSparks.Core.GSRequestData();
status = "invalid action"; // action variable isn't valid, check spelling or value
}
Spark.setScriptError("action", status); // set an error to prevent the AuthenticationRequest being processed
}
function startRecovery(request){
if(!request.email){
status = "email variable not passed in"; //Either email variable was not passed in pr spelt incorrect
return;
}
var players = Spark.systemCollection("player");
var playerId = players.findOne({"scriptData.email" : request.email}, {_id : 1, displayName:1}); // requires an 'email' value to be set on each player via setScriptData, and an index to exist on scriptData.email
if(!playerId){
status = "invalid"; //Email is incorrect or account isn't linked to one
return;
}
var token = generateRecoveryToken(); // Function to generate a unique token
var player = Spark.loadPlayer(playerId._id.$oid);
player.setScriptData("passwordRecoveryToken", token); //Sends the token back with the response
sendRecoveryEmail(request.email, playerId.displayName, token); // Function used to send email
status = "complete"; // Successfully found player and attempted to send email
}
function generateRecoveryToken(){
return "randomlyGeneratedToken-" + new Date().getTime();} // this should be cryptographically strong, not simply date-based
Rich Joslin
Having all these different ways of reading parameters is very confusing. When do we use each one? I can't find clear documentation on the differences.
Related problem:
I'm sending an AcceptChallengeRequest from Unity, and I am doing this to set the script data:
So I would think I would read that in the Cloud Code by using Spark.getScriptData(), but that's not working.
I have tried all of these to read the parameter in the AcceptChallengeRequest cloud script, and they are all always undefined or null:
What am I doing wrong?
But guess what, it works if you use code like this:
Spark.getData().scriptData.param1
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstRich Joslin
And I have confirmed that the script data is getting sent correctly from Unity:
Customer Support
So in order to get the script-data from that request you will need to use Spark.getData().getScriptData(), and then your attribute (i.e. Spark.getData().getScriptData().attribute).
So you need to access the data coming from the request and that data contains the script data.
There isnt any difference between Spark.data and Spark.getData()
Spark.data was an old call we dont use anymore, but it is still possible to use it.
Hope that helps,
Sean
Kristaps Auzins
Right now tryed this stuff and it does not work :(
Made function in cloud code:
Spark.getData().getScriptData().param1
But when I run it in Test Harness, I get error:
Function getScriptData does not exist.:
How so???
In JSON data is set:
"scriptData":{"param1":"1"}
Kristaps Auzins
But guess what, it works if you use code like this:
Spark.getData().scriptData.param1
Customer Support
Thats my bad, sorry.
getScriptData() will only work for a few objects like the player.
In other cases it will just appear as an attribute of getData().
That was an oversight on my part.
Does this work for you now?
Sean
Rich Joslin
Kristaps' answer worked for me. Thank you both!
James Gregory
I have a similar problem, when i send over scriptdata from unity, it sends everything fine. However when i try to check if it contains anything in the authentication request cloud code, it never does.
If i use the test harness and send over a "passwordRecoveryRequest" action the code gets used appropriately. Any help?
Unity Code authentication:
public void ForgottonPassword()
{
//Create a data object
GameSparks.Core.GSRequestData scriptData = new GameSparks.Core.GSRequestData();
scriptData.AddString("action", "passwordRecoveryRequest");
//Add a string variable with the key 'email'
scriptData.AddString("email", "example@gamesparks.com");
new AuthenticationRequest().SetScriptData(scriptData).Send((response) =>
{
if (!response.HasErrors)
{
Debug.Log("Forgotton Password COMPLETE");
}
else
{
Debug.Log("Password Reset Error" + response.Errors.JSON.ToString());
}
});
}
cloud code authentication:
var status = "Started";
if(Spark.data.scriptData){ //Checking if there is any scriptData passed in, if not then carry on the authentication as normal
var action = Spark.data.scriptData.action;
if("passwordRecoveryRequest" === action){
startRecovery(Spark.data.scriptData); //Start recovery sequence
} else if ("resetPassword" === action){
resetPassword(Spark.data.scriptData); //Start reset sequence
}
else
{
status = "invalid action"; // action variable isn't valid, check spelling or value
}
Spark.setScriptError("action", status); // set an error to prevent the AuthenticationRequest being processed
}
function startRecovery(request){
if(!request.email){
status = "email variable not passed in"; //Either email variable was not passed in pr spelt incorrect
return;
}
var players = Spark.systemCollection("player");
var playerId = players.findOne({"scriptData.email" : request.email}, {_id : 1, displayName:1}); // requires an 'email' value to be set on each player via setScriptData, and an index to exist on scriptData.email
if(!playerId){
status = "invalid"; //Email is incorrect or account isn't linked to one
return;
}
var token = generateRecoveryToken(); // Function to generate a unique token
var player = Spark.loadPlayer(playerId._id.$oid);
player.setScriptData("passwordRecoveryToken", token); //Sends the token back with the response
sendRecoveryEmail(request.email, playerId.displayName, token); // Function used to send email
status = "complete"; // Successfully found player and attempted to send email
}
function generateRecoveryToken(){
return "randomlyGeneratedToken-" + new Date().getTime();} // this should be cryptographically strong, not simply date-based
-
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 2486 topics