Sign In Register

How can we help you today?

Start a new topic

When to use: Spark.getScriptData / / Spark.getData() ?

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:  

Dictionary<string, object> scriptDataDict = new Dictionary<string, object>();
scriptDataDict["selectedDeckId"] = "someDeckIdHere";
new AcceptChallengeRequest()
.SetScriptData(new GSRequestData(scriptDataDict))

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?

Best Answer

But guess what, it works if you use code like this:

And I have confirmed that the script data is getting sent correctly from Unity:

GS: SEND:{"@class":".AcceptChallengeRequest","challengeInstanceId":"56...7a","scriptData":{"selectedDeckId":"deck01"},"requestId":"63...20_7"}


Thats a good question Rich.

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 and Spark.getData() was an old call we dont use anymore, but it is still possible to use it.

Hope that helps,


Right now tryed this stuff and it does not work :(

Made function in cloud code:

But when I run it in Test Harness, I get error:

Function getScriptData does not exist.:

How so???

In JSON data is set:


But guess what, it works if you use code like this:

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?



Kristaps' answer worked for me.  Thank you both!

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", "");

        new AuthenticationRequest().SetScriptData(scriptData).Send((response) =>


            if (!response.HasErrors)


                Debug.Log("Forgotton Password COMPLETE");




                Debug.Log("Password Reset Error" + response.Errors.JSON.ToString());




cloud code authentication: 

var status = "Started";

    if({ //Checking if there is any scriptData passed in, if not then carry on the authentication as normal

        var action =;

        if("passwordRecoveryRequest" === action){

            startRecovery(; //Start recovery sequence

        } else if ("resetPassword" === action){

            resetPassword(; //Start reset sequence




            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){


            status = "email variable not passed in"; //Either email variable was not passed in pr spelt incorrect



        var players = Spark.systemCollection("player");

        var playerId = players.findOne({"" :}, {_id : 1, displayName:1}); // requires an 'email' value to be set on each player via setScriptData, and an index to exist on


            status = "invalid"; //Email is incorrect or account isn't linked to one



        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(, 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

Login to post a comment