Sign In Register

How can we help you today?

Start a new topic

Getting a list of all players in Cloud Code

Hi, my name is Víctor and I would like to know how to iterate over a list with all existing players on my game...

What I want to achieve is something like this in order to execut this in GSMinute System Event.

Any advise ?


//Attempt to get entry
var API = Spark.getGameDataService();

    Spark.setScriptError("ERROR", entryObject.error())
} else{

    var playerIds = Spark.getPlayerIds(); //HERE I WANT TO GET ALL PLAYERS
    var player = Spark.loadPlayer(playerIds[0]); //LOAD FIRST PLAYER
    var entryObject = API.getItem("Pet", player.getUserName()); //GETTING HIS PET
    var entry = entryObject.document();
    entry.getData().characterStats["Hapiness"]=Hapiness-1; //UPDATING THE HAPINESS
    //Persist and return any errors
    var status = entry.persistor().persist().error();

        Spark.setScriptError("ERROR", status);



1 Comment

Hi Victor,

You could use a Bulk Job to update all player records at one time. You can use this in your GSMinute to submit a job that will affect all players. Note you may only have one bulk job per system script. Use {} as the query to apply this to all players in your system collection. The module to use would query game data services update the field with the given id matching the player's happiness similar to the code you have above. 

//GS Minute

Spark.getBulkScheduler().submitJobModule({},  "Your module name", Spark.getData(),0);

Just note as your player base grows this may not be an ideal solution as it takes time to do bulk jobs depending on the operation and if one is not finished when another is supposed to be scheduled, the new one won't be scheduled. If this happens you will need to look at a different way to efficiently updating player records. For example only updating active players instead of all players,performing a bulk job every hour or updating individual stats via the client when the player is playing etc. 

If you have any other questions please don't hesitate to contact us.



Login to post a comment