Sign In Register

How can we help you today?

Start a new topic
Answered

Storing player entities generated during gameplay

Hi. I'm trying to save player generated entities (buildings, units, sectors), but I need those to have unique ID (preferably created by the platform), so the client only handles random strings. To achieve this, i created a runtime collection and add entities like so:

var dCollection = Spark.runtimeCollection("player_progress");
var json = Spark.data.VALUE;
var entry = {"playerId" : Spark.getPlayer().getPlayerId(), "data":json};
var success = dCollection.insert(entry);

When I want to retrieve an entity, I use:  

var dCollection = Spark.runtimeCollection("player_progress");
var docId = Spark.data.ID;
var doc = dCollection.findOne({"_id":{"$oid": docId}});
Spark.setScriptData("item", doc);

Is this the correct approach?

I am trying to modify an entry, with: 

var dCollection = Spark.runtimeCollection("player_progress");
var docId = Spark.data.ID;
var field = Spark.data.FIELD;
var doc = dCollection.findAndModify( <missing code to find doc with id and modify field>  );
dCollection.save(doc);

Could you please show an example of how to modify the doc with given docId (playerId is irrelevant here) and field. The field is a number and would increase by 1.


Thanks!


Best Answer

I was able to do it with:

var doc = dCollection.findOne({"_id":{"$oid": docId}});
doc.eData.ProductionPerMin = 2;
var success = dCollection.update({"_id":{"$oid": docId}}, doc);

Still not sure if it's a good approach. It would mean this collection would have a lot of documents with only amount of data in it. Is there a way to store this against the player, but to have a unique id for each entry?


Answer

I was able to do it with:

var doc = dCollection.findOne({"_id":{"$oid": docId}});
doc.eData.ProductionPerMin = 2;
var success = dCollection.update({"_id":{"$oid": docId}}, doc);

Still not sure if it's a good approach. It would mean this collection would have a lot of documents with only amount of data in it. Is there a way to store this against the player, but to have a unique id for each entry?

 Hey Paavo,

Using update is a good alternative, but you should use $set instead of updating the whole doc. This will allow you to update only certain part of the doc rather then the whole thing.

One way you could reduce the amount of data in each doc could be to store separate entities in different collections, but without knowing how you access them and how often its hard to advise.

Login to post a comment