Sign In Register

How can we help you today?

Start a new topic
Answered

trying to achieve a leveling system with level achievement.

my leveling system works but each time i run the cloud code to check if i level up when received xp, i try to send a check on achievement from the Earn XP event, which get no response, if i do it from test harness it just work


my EarnXp event cloudCode:


 

//the xp we want to add
var receivedXp = Spark.getData().xp;
var leveledUp = false;

var currentXp = Spark.getPlayer().getScriptData("current_experience");
var requiredXp = Spark.getPlayer().getScriptData("required_experience");
var currentLevel = Spark.getPlayer().getScriptData("current_level");

currentXp = currentXp + receivedXp;

//check if we level up
if(currentXp >= requiredXp)
{
    leveledUp = true;
    //increment level
    currentLevel++;
    //deal with over xp
    var overXp = currentXp - requiredXp;
    
    // get new requiredXp and current
    requiredXp = currentLevel*currentLevel*50;
    currentXp = overXp;
    
    //check achievement
    Spark.sendRequest({
     "@class": ".LogEventRequest",
     "eventKey": "CheckAchievementLevel",
     "level": currentLevel
    });

    //write new values
    Spark.getPlayer().setScriptData("required_experience", requiredXp);
    Spark.getPlayer().setScriptData("current_level", currentLevel);
}

//write our final xp
Spark.getPlayer().setScriptData("current_experience", currentXp);

//send our result
Spark.setScriptData("new_current_experience", Spark.getPlayer().getScriptData("current_experience"));
Spark.setScriptData("new_required_experience", Spark.getPlayer().getScriptData("required_experience"));
Spark.setScriptData("Level_up", leveledUp);
Spark.setScriptData("current_level", currentLevel);

 

my checkAchivementLevel event


 

var currentLevel = Spark.getData().level;


if(currentLevel == 5)
{
    var player = Spark.getPlayer();
    player.addAchievement("level_5");
}

 


so in test harness im adding xp until i reach level 5, i dont get the achievement award message back

like that:


 

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "Level_up": true,
  "current_level": 5,
  "new_required_experience": 1250,
  "new_current_experience": 436
 }
}
{
 "@class": ".LogEventRequest",
 "eventKey": "EarnExperience",
 "xp": 601
}
{
 "@class": ".LogEventResponse",
 "scriptData": {
  "Level_up": false,
  "current_level": 4,
  "new_required_experience": 800,
  "new_current_experience": 635
 }
}

 

However i get the message if i manually call the event CheckAchievementLevel myself:


 

{
 "@class": ".LogEventResponse",
 "scriptData": null
}
{
 "@class": ".AchievementEarnedMessage",
 "messageId": "56703962e4b03618c469a733",
 "notification": true,
 "summary": "You just earned level 5",
 "achievementShortCode": "level_5",
 "currency1Earned": "25",
 "achievementName": "level 5",
 "playerId": "567037aee4b03618c4698ee5"
}
{
 "@class": ".LogEventRequest",
 "eventKey": "CheckAchievementLevel",
 "level": 5
}

 


Best Answer

yes if that interest someone here is how i did it:


 earnxp event

//the xp we want to add
var receivedXp = Spark.getData().xp;
var leveledUp = false;
 var properties = Spark.getProperties().getProperty("config");
var currentXp = Spark.getPlayer().getScriptData("current_experience");
var requiredXp = Spark.getPlayer().getScriptData("required_experience");
var currentLevel = Spark.getPlayer().getScriptData("current_level");

currentXp = currentXp + (receivedXp * properties.xp_factor ) ;

//check if we level up
if(currentXp >= requiredXp)
{
    leveledUp = true;
    //increment level
    currentLevel++;
    //earn stats and skill points
   
    
    var currentStatPoints = Spark.getPlayer().getScriptData("stat_points_left");
    currentStatPoints += properties.stat_point_per_level;
    
     var currentSkillPoints = Spark.getPlayer().getScriptData("skill_points_left");
    currentSkillPoints += properties.skill_points_per_level;
    //deal with over xp
    var overXp = currentXp - requiredXp;
    
    // get new requiredXp and current
    requiredXp = currentLevel*currentLevel*50;
    currentXp = overXp;
    Spark.getPlayer().setScriptData("current_level", currentLevel);
    
    //check achievement
    var req = new SparkRequests.LogEventRequest();
    req.eventKey = "check_achievement_level";
    var resp = req.Execute();

    //write new values =>server
    Spark.getPlayer().setScriptData("required_experience", requiredXp);
    Spark.getPlayer().setScriptData("current_level", currentLevel);
    Spark.getPlayer().setScriptData("stat_points_left", currentStatPoints);
    Spark.getPlayer().setScriptData("skill_points_left", currentSkillPoints);
    
    //add to response (only if level up)
    Spark.setScriptData("stat_points_left", currentStatPoints);
    Spark.setScriptData("skill_points_left", currentSkillPoints);
}

//write our final xp
Spark.getPlayer().setScriptData("current_experience", currentXp);

//send our result => response
Spark.setScriptData("new_current_experience", Spark.getPlayer().getScriptData("current_experience"));
Spark.setScriptData("new_required_experience", Spark.getPlayer().getScriptData("required_experience"));
Spark.setScriptData("level_up", leveledUp);
Spark.setScriptData("current_level", currentLevel);

 

 check achievement level

var currentLevel = Spark.getPlayer().getScriptData("current_level");


if(currentLevel == 5)
{
    var player = Spark.getPlayer();
    player.addAchievement("level_5");
}

 


Hey Damien,

Excuse me, just so i can get this straight in my head. Am i right in thinking that the problem is that you award an achievement in 'CheckAchievementLevel' event, which should send a message, but in the client you receive no message?

Sean

 

yes exactly, i cant trigger the achivement award e=message if the achievement is awarded through an other log event

Okay Damien,

Well since you see it happen in the test-harness, your code is definitely working, so that's good (btw, in the test harness does the message always get send 100% of the time?).

So do you have a message listener setup in your client?
What client are you using btw?

Thanks,
Sean

 

not sure if my last message have been posted?


I have tried in both test harness and unity client, i call EarnXP( 300) until i reach level 5.


in the test harness you can see what i did:

im level 4 => earnXp logEvent => reach level 5

https://gyazo.com/a4cb61ed65de7a021651bbe61765949b

from here i should trigger the AchievementEarnedMessage but it does not get call from my cloud code for some reason i do not understand.

so i call the log event check_achievement_level myself => orange message  AchievementEarnedMessage appear.

Sorry Damien, my mistake, i misunderstood where the achievement response came in.

When you step through the event in cloud-code can you see receivedXp, currentXp , requiredXp and currentLevel to have the right values?

Sean



 

Hi,


Cloud code is not executed on requests made from Cloud code. You do not see your AchievementEarnedMessage because the Cloud code for your CheckAchievementLevel is never executed. What you can do is move your CheckAchievementLevel code to Module and include that Module in your EarnXp event.


See the following threads for more information:


Event cloud code not running when called from cloud code

Cloud code call from other cloud call event


Marcel

Sorry again Damien, ive doubly misunderstood what you were doing, apologies again.

 

But i have a solution for both yourself and Marcel, and i can confirm that you can send LogEventRequests from another LogEventRequest, only you don't do so using Spark.sendRequest. Here is some documentation here that will give you a bit more information.


I'll give you an example here anyway, but refer back to the docs if you want to expand upon this....

So, i have a really simple event which will add an achievement to a player. I've called it sendTestAch, and it contains only the following code...

 

Spark.getPlayer().addAchievement("testAch");

 

Then i call this logevent from another logevent which contains the following code...

 

var req = new SparkRequests.LogEventRequest();
req.eventKey = "sendTestAch";
var resp = req.Execute();

 

And running this in the test harness you can step through the code and when the Execute() command is triggered you will jump into the sendTestAch event and you will be able to step through that. You should see your achievement message being received in the Log.


This should works for you as your code looks fine to me, it was just this problem with the Spark.sendRequest, as Marcel pointed out (thanks btw Marcel, keep it up for MAU rewards!).

Let me know if this works for you,
Sean




thanks, im going to try that!

Hey Damien,

Did that work for you?

 

Answer

yes if that interest someone here is how i did it:


 earnxp event

//the xp we want to add
var receivedXp = Spark.getData().xp;
var leveledUp = false;
 var properties = Spark.getProperties().getProperty("config");
var currentXp = Spark.getPlayer().getScriptData("current_experience");
var requiredXp = Spark.getPlayer().getScriptData("required_experience");
var currentLevel = Spark.getPlayer().getScriptData("current_level");

currentXp = currentXp + (receivedXp * properties.xp_factor ) ;

//check if we level up
if(currentXp >= requiredXp)
{
    leveledUp = true;
    //increment level
    currentLevel++;
    //earn stats and skill points
   
    
    var currentStatPoints = Spark.getPlayer().getScriptData("stat_points_left");
    currentStatPoints += properties.stat_point_per_level;
    
     var currentSkillPoints = Spark.getPlayer().getScriptData("skill_points_left");
    currentSkillPoints += properties.skill_points_per_level;
    //deal with over xp
    var overXp = currentXp - requiredXp;
    
    // get new requiredXp and current
    requiredXp = currentLevel*currentLevel*50;
    currentXp = overXp;
    Spark.getPlayer().setScriptData("current_level", currentLevel);
    
    //check achievement
    var req = new SparkRequests.LogEventRequest();
    req.eventKey = "check_achievement_level";
    var resp = req.Execute();

    //write new values =>server
    Spark.getPlayer().setScriptData("required_experience", requiredXp);
    Spark.getPlayer().setScriptData("current_level", currentLevel);
    Spark.getPlayer().setScriptData("stat_points_left", currentStatPoints);
    Spark.getPlayer().setScriptData("skill_points_left", currentSkillPoints);
    
    //add to response (only if level up)
    Spark.setScriptData("stat_points_left", currentStatPoints);
    Spark.setScriptData("skill_points_left", currentSkillPoints);
}

//write our final xp
Spark.getPlayer().setScriptData("current_experience", currentXp);

//send our result => response
Spark.setScriptData("new_current_experience", Spark.getPlayer().getScriptData("current_experience"));
Spark.setScriptData("new_required_experience", Spark.getPlayer().getScriptData("required_experience"));
Spark.setScriptData("level_up", leveledUp);
Spark.setScriptData("current_level", currentLevel);

 

 check achievement level

var currentLevel = Spark.getPlayer().getScriptData("current_level");


if(currentLevel == 5)
{
    var player = Spark.getPlayer();
    player.addAchievement("level_5");
}

 

Great!

Thanks for sharing the solution Damien.

 

Login to post a comment