Sign In Register

How can we help you today?

Start a new topic
Answered

Cloud Code Functions

 Hello community,


i want to create a server side script to generate a custom value and return it to the current user.

This is the workflow:


LogEventRequest -> generateCustomValue ( userName, testValue )


Cloud Code -> generateCustomValue -> getRequest(userName, testValue ) -> create Value for this User -> return Value


Can somebody plz help me and show how i can do this? Maybe with a small example ?


Thanks !


Best Answer

Hi Eugen,


Here's an event that takes a string (could also be a number or JSON):



The important thing here is that the Default Calc is set to "Used In Script".


Then in the Cloud Code for "generateCustomValue" I have this code:


  

var customValueToStore = Spark.getData().customValue; //this will get the attribute of the event

var thisPlayer = Spark.getPlayer(); //This will load this user's player object

var thisPlayersName = thisPlayer.getDisplayName(); //This will load the user's displayname

thisPlayer.setScriptData("customValue", customValueToStore); //This will save customValueToStore to the player object's scriptData

//Spark.setScriptData("customValue", customValueToStore); //This will save the custom value to the Response which will be visible on the Inspector in the Test Harness

addToName(thisPlayersName, customValueToStore); //Run the function below


function addToName(playerName, customValueToAdd){
    var newCustomValue = playerName + " " + customValueToAdd; //add the two variables from above together
    
    Spark.setScriptData("AddedValues", newCustomValue); //This will save the added values to the Response which will be visible on the Inspector in the Test Harness
}

  

The key thing to notice here is setScriptData. Spark.setScriptData will ONLY set the ScriptData of the response, which is useful for returning data from a custom event that is immediately usable by whatever game engine you are using.


Spark.getPlayer().setScriptData sets the ScriptData to that player's playerObject, it wont be surfaced in the response unless you use Spark.scriptData also.


Then in the Test Harness I sent the LogEventRequest:


{
 "@class": ".LogEventRequest",
 "eventKey": "generateCustomValue",
 "customValue": "thinks GameSparks is awesome!"
}

 

And received the response:

 

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "AddedValues": "shane thinks GameSparks is awesome!"
 }
}

 

You can now also send a "getAccountDetails" request and see that we've also stored the Custom Value to the player's scriptData:


 

Request:
{
 "@class": ".AccountDetailsRequest"
}

Response:
{
 "@class": ".AccountDetailsResponse",
 "currency1": 0,
 "currency2": 0,
 "currency3": 0,
 "currency4": 0,
 "currency5": 0,
 "currency6": 0,
 "displayName": "shane",
 "location": {
  "latitide": 53.202805,
  "longditute": -6.098297,
  "city": "Bray",
  "country": "IE"
 },
 "scriptData": {
  "customValue": "thinks GameSparks is awesome!"
 },
 "userId": "54cb5fffe4b08fe2e9f7ec61"
}

 

Hope this answers your question,

Shane

1 Comment

Answer

Hi Eugen,


Here's an event that takes a string (could also be a number or JSON):



The important thing here is that the Default Calc is set to "Used In Script".


Then in the Cloud Code for "generateCustomValue" I have this code:


  

var customValueToStore = Spark.getData().customValue; //this will get the attribute of the event

var thisPlayer = Spark.getPlayer(); //This will load this user's player object

var thisPlayersName = thisPlayer.getDisplayName(); //This will load the user's displayname

thisPlayer.setScriptData("customValue", customValueToStore); //This will save customValueToStore to the player object's scriptData

//Spark.setScriptData("customValue", customValueToStore); //This will save the custom value to the Response which will be visible on the Inspector in the Test Harness

addToName(thisPlayersName, customValueToStore); //Run the function below


function addToName(playerName, customValueToAdd){
    var newCustomValue = playerName + " " + customValueToAdd; //add the two variables from above together
    
    Spark.setScriptData("AddedValues", newCustomValue); //This will save the added values to the Response which will be visible on the Inspector in the Test Harness
}

  

The key thing to notice here is setScriptData. Spark.setScriptData will ONLY set the ScriptData of the response, which is useful for returning data from a custom event that is immediately usable by whatever game engine you are using.


Spark.getPlayer().setScriptData sets the ScriptData to that player's playerObject, it wont be surfaced in the response unless you use Spark.scriptData also.


Then in the Test Harness I sent the LogEventRequest:


{
 "@class": ".LogEventRequest",
 "eventKey": "generateCustomValue",
 "customValue": "thinks GameSparks is awesome!"
}

 

And received the response:

 

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "AddedValues": "shane thinks GameSparks is awesome!"
 }
}

 

You can now also send a "getAccountDetails" request and see that we've also stored the Custom Value to the player's scriptData:


 

Request:
{
 "@class": ".AccountDetailsRequest"
}

Response:
{
 "@class": ".AccountDetailsResponse",
 "currency1": 0,
 "currency2": 0,
 "currency3": 0,
 "currency4": 0,
 "currency5": 0,
 "currency6": 0,
 "displayName": "shane",
 "location": {
  "latitide": 53.202805,
  "longditute": -6.098297,
  "city": "Bray",
  "country": "IE"
 },
 "scriptData": {
  "customValue": "thinks GameSparks is awesome!"
 },
 "userId": "54cb5fffe4b08fe2e9f7ec61"
}

 

Hope this answers your question,

Shane


1 person likes this
Login to post a comment