Sign In Register

How can we help you today?

Start a new topic
Answered

How to get value from a field in player collection that I can manipulate in cloud code

Above you see a player from my player collection. I am making an event called updateWorld with a bunch of attributes : worldNum, ore, distanceTraveled, highscore as numbers ...and rocketParts as JSON. 

In the cloudcode I then take the params to construct the fields in the player's document as you can see above, like so:


CloudCode:

var world = "world";

var num = Spark.data.worldNumber;

var worldNumString = world.concat(num); //constructs the world number key


var distanceTraveled = Spark.data.distanceTraveled; 

var ore = Spark.data.ore; 

var unlocked = Spark.data.unlocked; 

var timesPlayed = Spark.data.timesPlayed; 

var highScore = Spark.data.highScore; 

var rocketParts = Spark.data.rocketParts; 

Spark.getPlayer().setScriptData(worldNumString,{

 "distanceTraveled":distanceTraveled, "ore": ore, "unlocked":unlocked, "timesPlayed":timesPlayed, "highScore":highScore,      "rocketParts":rocketParts});


What needs to happen next:  

I need to now have the cloudcode first check the player collection and add the values from there, with the values coming in from the game.


So I tried adding this to the cloud code:

 

var gsOre = Spark.getPlayer().getScriptData("ore"); // Get the amount  of ore that is already in the field

var ore = Spark.data.ore;  // Get the amount of ore coming in from game

var totalOre = gsOre + ore; // add it up


Problem: gsOre is null. 

 

Can someone help me see what I need to do to get the value from my document so that I can add it and replace the new value?


Thank you,


Maria




Best Answer

Hi Maria,


It looks like the ore property is nested in another document


var gsOre = Spark.getPlayer().getScriptData("world1")["ore"]


Try this and see if the variable is still null. If this doesn't work I'll investigate further


-Pádraig


Yes! Thank you Pádraig! 

Still learning the syntax :)

Answer

Hi Maria,


It looks like the ore property is nested in another document


var gsOre = Spark.getPlayer().getScriptData("world1")["ore"]


Try this and see if the variable is still null. If this doesn't work I'll investigate further


-Pádraig

Login to post a comment