Sign In Register

How can we help you today?

Start a new topic

Optimizing inventory Query

I was working on creating a list of items that was bought inside the game, and i  followed this tutorial, am not convinced this is the best way to do what i want because i have to go through all fields in the runtime collection and pick the ones only related to the player, is there a way to organize the data in a way so that i can add list under each user ID, in general is this the best way to store a list relative only to the player, Thanks


Hi Ahmed,

You might have a look at our documentation regarding Managing Data Persistence here (https://docs.gamesparks.com/tutorials/database-access-and-cloud-storage/managing-data-persistence.html#indexing-custom-collections), this should help explain how to improve query performance. Depending on the amount and structure of this data, one way might be to have the playerId as the key of an object array, this way when you query the playerId it would return only the data related to that player. Another simpler option here might be to store this in player private / script data. We have some documentation here on this (https://docs.gamesparks.com/api-documentation/cloud-code-api/player/sparkplayer.html#setprivatedata) this would make retrieving this data as simple as var data = Spark.getPlayer().getPrivateData("KEY"); I hope this helps.

Kind regards,
 - Steve

 

Thanks Steve, Can i create a list with player data, i mean using insert new field, or removing a specific field, or i have to send the whole data as a string and convert it to array via code??

 Hi Ahmed,

That should be perfectly possible. One way you might go about it is like so below. This would be similar for arrays as well, where you could use push to add to an array or slice to make a new array minus the data you wish to remove. Does this make sense? I hope this helps.

Kind regards,
 - Steve

// Add to a document
var player = Spark.getPlayer();
var playerData = player.getPrivateData("dataKey");
playerData.newKey = "myNewListData";
player.setPrivateData("dataKey", playerData);

// Remove from a document
var player = Spark.getPlayer();
var playerData = player.getPrivateData("dataKey");
delete playerData.keyToRemove;
player.setPrivateData("dataKey", playerData);

Hello Kevin, 

I tried your script but it returns error in the test harness because player data is null, even if i set player data it still return only on field, also i read that privatedata are not suppose to be send to the device, and i need to send the whole inventory to the player when he start the game. 

Thanks,

Can you explain me more how to set the playerID as a key to an object array in the runtime Collections. i tried Spark.runtimeCollection("playerInventory/" + playerID)  Didn't work it create a new Collection every time, and this is not what i intended, i wanted to create a hierarchy as Collection/playerID/ fields of user data.

Hi Ahmed,

One way you could go about that is like so, first you'd create a single collection "playerData" for instance. You could then construct your document like so.

var doc = {};
doc.playerId = Spark.getPlayer().getPlayerId();
doc.list = []; // This would be

Spark.runtimeCollection("playerData").insert(doc);

This will then produce a document in NoSQL that looks like so, where the list property would be the array, does this make sense?

{
    "playerId": "123",
    "list": []
}

From that you can then access this information like so.

var playerData = Spark.runtimeCollection("playerData").findOne({"playerId": "123"});

In order to have the playerId as the key you can do so like so.

var doc = {};
doc[Spark.getPlayer().getPlayerId()] = []

I hope this helps. Kind regards,
 - Steve

 

Login to post a comment