trying to achieve a leveling system with level achievement.
d
damien delmarle
started a topic
about 3 years ago
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
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");
}
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
d
damien delmarle
said
about 3 years ago
yes exactly, i cant trigger the achivement award e=message if the achievement is awarded through an other log event
Customer Support
said
about 3 years ago
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
d
damien delmarle
said
about 3 years ago
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.
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.
Customer Support
said
about 3 years ago
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
M
Marcel Piestansky
said
about 3 years ago
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.
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
d
damien delmarle
said
about 3 years ago
thanks, im going to try that!
Customer Support
said
about 3 years ago
Hey Damien,
Did that work for you?
d
damien delmarle
said
about 3 years ago
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");
}
damien delmarle
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:
my checkAchivementLevel event
so in test harness im adding xp until i reach level 5, i dont get the achievement award message back
like that:
However i get the message if i manually call the event CheckAchievementLevel myself:
yes if that interest someone here is how i did it:
earnxp event
check achievement level
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
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
damien delmarle
yes exactly, i cant trigger the achivement award e=message if the achievement is awarded through an other log event
Customer Support
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
damien delmarle
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.
Customer Support
When you step through the event in cloud-code can you see receivedXp, currentXp , requiredXp and currentLevel to have the right values?
Sean
Marcel Piestansky
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
Customer Support
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...
Then i call this logevent from another logevent which contains the following code...
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
damien delmarle
thanks, im going to try that!
Customer Support
Did that work for you?
damien delmarle
yes if that interest someone here is how i did it:
earnxp event
check achievement level
Customer Support
Thanks for sharing the solution Damien.
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2339 topics