Sign In Register

How can we help you today?

Start a new topic
Answered

sendRequest not doing LogEventRequest

 I've got two events with cloud code scripts. I'm trying to use Spark.sendRequest to send a LogEventRequest for the second event from inside the first event's script:


UseStamina: This event calls Spark.sendRequest to also process UpdateStamina part way through the event

// ====================================================================================================
//
// Cloud Code for UseStamina, write your code here to customise the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://portal.gamesparks.net/docs.htm			
//
// ====================================================================================================

var player = Spark.getPlayer();

var stamina = player.getScriptData("stamina");

if ( stamina === stamina ) {
    stamina = 0;
}

var scheduler = Spark.getScheduler();

var response = Spark.sendRequest({
 "@class": ".LogEventRequest",
 "eventKey": "UpdateStamina"
});

stamina = player.getScriptData("stamina");

Spark.setScriptData("response", response);
Spark.setScriptData("stamina", stamina);

  UpdateStamina increases the amount of stamina the player currently has by 5 and returns the stamina in the event response. 

// ====================================================================================================
//
// Cloud Code for UpdateStamina, write your code here to customise the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://portal.gamesparks.net/docs.htm			
//
// ====================================================================================================
var player = Spark.getPlayer();

var stamina = player.getScriptData("stamina");

if (stamina === null) {
    stamina = 0;
}

if ( stamina < 20 ) {
    stamina += 5;
    player.setScriptData("stamina", stamina);
}

Spark.setScriptData("stamina", stamina);

  

When I call just UpdateStamina the stamina value increments properly and appears to be fine.


When I use the test harness to send UseStamina I get this response every time: 

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "response": {
   "@class": ".LogEventResponse",
   "scriptData": null
  },
  "stamina": 0
 }
}

The script data for the response is null when it SHOULD have the stamina value set in the UpdateStamina event.

Also the stamina is not incrementing so it seems that the UpdateStamina event is not running at all.


What's going on here? Why isn't sendRequest working? 


Best Answer

Hey Cory,

The standard Gamesparks events can be called using the Spark.sendRequest() method, however custom events like log-events cannot be called be called in this way, so you will always get a null-script data response.


An alternative to calling log-events is to put your stamina-update code inside a function, in a custom module.
In your cloud-code you will see a section for that. You can write your update-function in there, just as you did in your log-event, but returning the value rather than sending it as script-data.

 

function updateStamina()
{
    var player = Spark.getPlayer();
 
    var stamina = player.getScriptData("stamina");
 
    if (stamina === null) {
        stamina = 0;
    }
 
    if ( stamina < 20 ) {
        stamina += 5;
        player.setScriptData("stamina", stamina);
    }
    return stamina;
}

 

Then in your UseStamina event, you can call that function by first including the Module at the top of your script (say your module was called "PLAYERMODULE")...


require("PLAYERMODULE");


Then just call the function as if it was in your script already...


var updatedStamina = updateStamina();


You can do the same in the log-event for UpdateStamina you already have, in case you want to call that log-event from your client-code.


I hope that helps,
- Sean


1 Comment

Answer

Hey Cory,

The standard Gamesparks events can be called using the Spark.sendRequest() method, however custom events like log-events cannot be called be called in this way, so you will always get a null-script data response.


An alternative to calling log-events is to put your stamina-update code inside a function, in a custom module.
In your cloud-code you will see a section for that. You can write your update-function in there, just as you did in your log-event, but returning the value rather than sending it as script-data.

 

function updateStamina()
{
    var player = Spark.getPlayer();
 
    var stamina = player.getScriptData("stamina");
 
    if (stamina === null) {
        stamina = 0;
    }
 
    if ( stamina < 20 ) {
        stamina += 5;
        player.setScriptData("stamina", stamina);
    }
    return stamina;
}

 

Then in your UseStamina event, you can call that function by first including the Module at the top of your script (say your module was called "PLAYERMODULE")...


require("PLAYERMODULE");


Then just call the function as if it was in your script already...


var updatedStamina = updateStamina();


You can do the same in the log-event for UpdateStamina you already have, in case you want to call that log-event from your client-code.


I hope that helps,
- Sean