Sign In Register

How can we help you today?

Start a new topic
Answered

GS Minute Not Responding

Hi, im trying to write a script to the GS Minute cloud code so that energy gets restored 1 point every minute until the user reaches 100. I have this written so far but nothing happens:


// ====================================================================================================

//

// Cloud Code for GS_MINUTE, write your code here to customise the GameSparks platform.

//

// For details of the GameSparks Cloud Code API see https://portal.gamesparks.net/docs.htm   

//

// ====================================================================================================

var Energy = Spark.getPlayer().getScriptData("energy");


if(parseInt(Energy) < 100){

  Spark.getPlayer().setScriptData("energy",(parseInt(Energy) + 1));

if(parseInt(Energy) > 100){

    Spark.getPlayer().setScriptData("energy", 100);

}

if(parseInt(Energy) < 0){

   Spark.getPlayer().setScriptData("energy", 0);

}




can anyone tell me what i did wrong? thanks in advance!


Best Answer
Hey there,

The GS_MINUTE script does not get called by the player. It just runs every minute. What you need is to traverse through all your players in your db, check if any of them have less than 100 energy, and then increment it by 1 until they hit 100.
// get all players from the db
var allMyPlayers = Spark.runtimeCollection("PlayerProfile").find();

// traverse through all players
while (allMyPlayers.hasNext()) {
    var myPlayer = allMyPlayers.next();
    
    // we either set or increment, or leave it as null
    // if the player's energy is already at 100
    var setModifier = null;
    
    // get the current energy. this will be null if the
    // energy field is not set
    var currentEnergy = myPlayer.energy;
    if (!currentEnergy) {
        // energy field not set so we set it to 1
        setModifier = { $set: { "energy" : 1 } };
    } else if (currentEnergy < 100) {
        // increment the energy by 1
        setModifier = { $inc: { "energy" : 1 } };
    }
    
    // each document in my collection has a player_id field set
    // so I make sure that it is valid or else mongo will throw an error
    if (setModifier && myPlayer.player_id) {
        // means there is a change that needs to be stored because we don't
        // need to make any modifications if the player's energy is already at 100
        Spark.runtimeCollection("PlayerProfile").update(
            { "player_id": myPlayer.player_id },
            setModifier);
    }
}

 You will need to modify the player_id field to match whats in your document, and the name of your collection containing the user's energy.

My PlayerProfile collection has the following format.

{
    "player_id" : "StarLord"
}

 Note that the code will set the energy field if it does not exist.

 

 


 


Answer
Hey there,

The GS_MINUTE script does not get called by the player. It just runs every minute. What you need is to traverse through all your players in your db, check if any of them have less than 100 energy, and then increment it by 1 until they hit 100.
// get all players from the db
var allMyPlayers = Spark.runtimeCollection("PlayerProfile").find();

// traverse through all players
while (allMyPlayers.hasNext()) {
    var myPlayer = allMyPlayers.next();
    
    // we either set or increment, or leave it as null
    // if the player's energy is already at 100
    var setModifier = null;
    
    // get the current energy. this will be null if the
    // energy field is not set
    var currentEnergy = myPlayer.energy;
    if (!currentEnergy) {
        // energy field not set so we set it to 1
        setModifier = { $set: { "energy" : 1 } };
    } else if (currentEnergy < 100) {
        // increment the energy by 1
        setModifier = { $inc: { "energy" : 1 } };
    }
    
    // each document in my collection has a player_id field set
    // so I make sure that it is valid or else mongo will throw an error
    if (setModifier && myPlayer.player_id) {
        // means there is a change that needs to be stored because we don't
        // need to make any modifications if the player's energy is already at 100
        Spark.runtimeCollection("PlayerProfile").update(
            { "player_id": myPlayer.player_id },
            setModifier);
    }
}

 You will need to modify the player_id field to match whats in your document, and the name of your collection containing the user's energy.

My PlayerProfile collection has the following format.

{
    "player_id" : "StarLord"
}

 Note that the code will set the energy field if it does not exist.

 

 


 


1 person likes this

thank you so much Davendra  for your very detailed response! i completely understand how it works now! one thing though, im still familiarizing myself with mongoDB so correct me if im wrong but basically, I need to create a collection called "PlayerProfile" and then within that collection create a document called "player_id"? 


thanks again in advance,

cj

thank you so much Davendra  for your very detailed response! i completely understand how it works now! one thing though, im still familiarizing myself with mongoDB so correct me if im wrong but basically, I need to create a collection called "PlayerProfile" and then within that collection create a document called "player_id"? 


thanks again in advance,

cj

That is correct. You need a runtimeCollection to store all your player's energy. If you have already created it, rename "PlayerProfile" to that collection's name.

Then if you already have a field in there that identifies a player, replace "player_id" with that field's name.

[Attached Screenshots]

 

png
png

1 person likes this

ok I see!! nice touch with the screen shots!! thank you very much! this whole BaaS portion of my current build has been a real challenge and I really, really appreciate your help Davandra!

-CJ