Sign In Register

How can we help you today?

Start a new topic
Answered

When to use: Spark.getScriptData / Spark.data / 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: 

Spark.getScriptData("selectedDeckId")
Spark.data.selectedDeckId
Spark.getData().selectedDeckId

 

What am I doing wrong?


Best Answer

But guess what, it works if you use code like this:
Spark.getData().scriptData.param1


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

 

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"}

Answer

But guess what, it works if you use code like this:
Spark.getData().scriptData.param1

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

 

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


Login to post a comment