Sign In Register

How can we help you today?

Start a new topic
Answered

Save multiple character against single player ID.

Hi,

  I am working on MMO game.I have to save multiple character and its info against single ID because in my game a player can save multiple heroes (can play from single one). I created an event, but when user told db to create character it simply update last information. 

var CharacterDataList = Spark.runtimeCollection("CharacterData");
    var playerID = Spark.getPlayer().getPlayerId();
    var characterSelected = Spark.getData().CS;
    var weaponLevel = Spark.getData().WL;
    var cname = Spark.getData().NAME;
    var wins = Spark.getData().WINS;
    var loss = Spark.getData().LOSS;
    var gold = Spark.getData().GOLD;
    var currentCharacter = {
        "playerID": playerID,
        "characterSelected": characterSelected,
        "weaponLevel": weaponLevel,
        "name": cname,
        "wins": wins,
        "loss": loss,
        "gold": gold
    }; // we construct a new player from the data we are about to input into the player data
    CharacterDataList.update({
        "playerID": playerID
    }, //Looks for a doc with the id of the current player
    {
        "$set": currentCharacter
    }, // Uses the $set mongo modifier to set old player data to the current player data
    true, // Create the document if it does not exist (upsert)
    false // This query will only affect a single object (multi)
    );

 Above code will run on cloud. How can I save new character against this id. I found a command of Insert but I don't know how to use it in my case to Add information.

Thanks in advance.

Regards.

Salam.


Best Answer
Hey Salam,

Looks like you've got most of the code right here.
The only only thing you'd need to adapt this to multiple characters is an id for each character.

So, your character JSON object would be.....

var currentCharacter = {
        "characterSelected": characterSelected,
        "weaponLevel": weaponLevel,
        "name": cname,
        "wins": wins,
        "loss": loss,
        "gold": gold

    };

And then when you insert you query playerId and characterId....

 CharacterDataList.update({"playerID": playerID, 'characterId' : characterId }, 
    {
        "$set": currentCharacter
    },true,false);

This will create a new doc for each character for each player. This isnt a bad idea either (thought it will create more docs), as it will be quicker to lookup and modify individual characters.

Does that make sense?
Sean







Answer
Hey Salam,

Looks like you've got most of the code right here.
The only only thing you'd need to adapt this to multiple characters is an id for each character.

So, your character JSON object would be.....

var currentCharacter = {
        "characterSelected": characterSelected,
        "weaponLevel": weaponLevel,
        "name": cname,
        "wins": wins,
        "loss": loss,
        "gold": gold

    };

And then when you insert you query playerId and characterId....

 CharacterDataList.update({"playerID": playerID, 'characterId' : characterId }, 
    {
        "$set": currentCharacter
    },true,false);

This will create a new doc for each character for each player. This isnt a bad idea either (thought it will create more docs), as it will be quicker to lookup and modify individual characters.

Does that make sense?
Sean






Sean,

  Thanks for reply. I just wondering currentcharacter variable is another collection?

I mean there will be another event containing information stored in currentcharacter. This currentcharacter is used in another event which just update information. Rt? Sean

I'd recommend using the character name for that ID. It just has to be anything unique to the character.
Theres no need to store it in another collection, it can be sent up with the main LogEventRequest

 

Thanks. It worked.

Login to post a comment