How to get value from a field in player collection that I can manipulate in cloud code
M
Maria Mercedes Martinez
started a topic
about 6 years ago
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;
Maria Mercedes Martinez
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
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
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
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
Maria Mercedes Martinez
Yes! Thank you Pádraig!
Still learning the syntax :)
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics