Sign In Register

How can we help you today?

Start a new topic
Answered

What is expected way to adding storage for game state?

I see that player documents are created by the system when a new user is created.  Is the schema/template for that initial document changeable?  Or are we expected to modify the document during player creation in cloud code?  Or is this even where I should store general game state that's outside of the gamesparks supported features?


Basically, I have a game that has complex game state (locations of many objects, states of quests, economic model, etc..) and I need a place to store and periodically update all that data.  Is the expected pattern to add my custom stuff to the player document (perhaps in the scriptData or privateData areas) or should I be making one or more new 'runtime' collections and then adding a field to the player document that points to the related game-state document?


Thanks.


Best Answer

Hi Steven,


For all my additional player data, what I did was just create additional runtime collections with a playerId field, and whenever I needed to load something for a player I would just use the spark() methods to get the current authenticated playerId. (Never pass the playerID as a variable from the client to the server).


So when a player registers, use the RegistrationResponse event to create all related player records in each collection, using the authenticated playerId.


Hope that helps.



Answer

Hi Steven,


For all my additional player data, what I did was just create additional runtime collections with a playerId field, and whenever I needed to load something for a player I would just use the spark() methods to get the current authenticated playerId. (Never pass the playerID as a variable from the client to the server).


So when a player registers, use the RegistrationResponse event to create all related player records in each collection, using the authenticated playerId.


Hope that helps.


Thanks for the response, that's helpful.


For playerId are you using the document "_id" value (I would assume yes)?  And then for the custom collections I assume you are embedding a specific playerId field into the document which references "_id" of the player document?  


How then are you creating an index to retrieve the full custom document(s) based on a query for the playerId field value?  I'm not a mongodb person but from a quick scan of the docs I don't see a way to create a secondary document index (one that is separate from the built-in _id document index).  I see that you can put an index on a sub-document, but that seems like quite a hack.  It seems that it's actually more natural (given how mongodb handles the document indices) to have a field array in the player document that would reference the "_id"s of the associated custom collection records. 


Last two questions:

1. Can you access all of the mongodb DDL-like calls from cloud script?  Things like createIndex..etc.

2. Should we avoid adding custom fields to the player document?  I don't know how gamesparks handles the management of that document.  Will it ever get bulk updated (like during a gamesparks framework upgrade)? 


Thanks!



Quick followup, that I see you can set an index on a field and do efficient document lookups that way.  Assuming you can create indices in cloudcode and also set the property of uniqueness on _id references, it should be fine to have user collection records reference the player collection records.  (I think.)

Hi Steven,


I'm not sure about setting indices etc in cloud code. I just use the Spark.getPlayer.getPlayerId() method to query the record in the collection that will get the record for the current player. 

I have another collection in my db called 'playerCustomData'. I use this collection to store anything for the player not in the player table. i.e config information, current game state, game settings etc. It's a very generic table for player specific stuff.


I access it with the following cloud code...

 

var playerId = Spark.getPlayer().getPlayerId();
var playerCustomData = Spark.runtimeCollection("playerCustomData").find({"playerid":playerId});

Spark.setScriptData("playerCustomData", playerCustomData);

 I also have other tables for more specfic data for the player, such as vehicles owned, decals owned etc. 


1 person likes this
Login to post a comment