Sign In Register

How can we help you today?

Start a new topic

Is there a way to wait for setScriptData() to end?

Hi, we were wondering if there would be a way to wait for setScriptData() to end writting in the data base so we can then execute another command.

Our problem is this:

We modify the ScriptData from a player and in the following command we send a message to him so he can reload his client with the refreshed information but that reload is done before the setScriptData has ended.



  

if(!response.error){
                var guildInfo = {
                    "guildName" : response.teamId,
                    "rank": "Member"
                };
                Spark.loadPlayer(pendingPlayerID).setScriptData("guild_info", guildInfo);
                
                Spark.runtimeCollection('pending_requests').findAndRemove({ "player_id" : pendingPlayerID});
                Spark.getLog().debug("Accepted" + joiningPlayer.error);
                Spark.setScriptData('accepted', 'Player joined');
                
                
                SendMessageReload();

 

 


2 people have this question

Hi Carlos Ruiz,


Changes made to the Player are persisted to the database at the end of the script - this is a performance enhancement, since often the player data is changed more than once in a single script.

This explains the behaviour you have been seeing.

If you need the data to be persisted on-demand, you can use the SparkPlayer.persist() method as documented here:


https://docs.gamesparks.com/api-documentation/cloud-code-api/player/sparkplayer.html#persist


So your code would need to look something like this:


var player = Spark.loadPlayer(pendingPlayerID);

player.setScriptData("guild_info", guildInfo); player.persist();

.

.

.

SendMessageReload();


Kind regards,


Jon.

Thank you for your fast answer, we have tried your solution :


 

if(!response.error){
               
                
                Spark.runtimeCollection('pending_requests').findAndRemove({ "player_id" : pendingPlayerID});
                Spark.getLog().debug("Accepted" + joiningPlayer.error);
                Spark.setScriptData('accepted', 'Player joined');
                
                
                 var guildInfo = {
                    "guildName" : response.teamId,
                    "rank": "Member"
                };
                
                var player1 = Spark.loadPlayer(pendingPlayerID);
                player1.setScriptData("guild_info", guildInfo);
                player1.persist();
                
                
                SendMessageReload();
                
                
            }

 

But we are still facing the same problem. We will leave the code we use to send the message in case it screws anything up, and the code we use to get that scriptData that is not "refreshed".




 

function SendMessageReload(){
    
    
    var msg = Spark.message("RELOAD_MEMBERS");
    
                var playerIDList = [];
            
                var i = 0;
    
                for each (var member in response.members){
                    
                    if(member.online){
                    
                        playerIDList[i] = member.id;
                        i++;
                        
                    }
                
                }

                msg.setPlayerIds(playerIDList);
                msg.send();
    
}

 


 

var playerAux = [];
    var ownerAux;
    var i = 0;
    
    if(!Spark.getPlayer().getScriptData("guild_info")){
        
        Spark.setScriptError('retry', "retry");
        throw new Error('retry');
    
    }else{
        
        
    
    for each (var playerReal in ownTeam.teams[0].members){
        
        playerAux[i] = { 
                    "player_id" : playerReal.id,
                    "displayName" : playerReal.displayName,
                    "level" : playerReal.scriptData.class.level,
                    "class_name": playerReal.scriptData.class.class_name,
                    "rank": playerReal.scriptData.guild_info.rank
                };
                i++;
        
    }

 

As you can see in the second snippet we have developed a way around to this question and we are seeing that it varies from working without retries to have to retry 4 times. In the client we reload again if we get the error "retry".

Login to post a comment