Sign In Register

How can we help you today?

Start a new topic
Answered

Sort Leaderboard by sum of two Event attributes

 Hi there,


I have a problem with leaderboards. Let's see if I manage to explain it well:
- I have two Events (Score1 and Score2), each of them has one attribute (SCORE1 AND SCORE2).

- I have three leaderboards that have to be sorted by these two events as follows:
    - Leaderboard1 is sorted by SCORE1.
    - Leaderboard2 is sorted by SCORE2.

    - Leaderboard3 is sorted by the result of this calculation: SCORE1 + SCORE2.


I know how to manage Leaderboard1 and 2, but I don't know how or where to calculate the sum. I've tried with cloud code, but I don't know how to access, from one event, variables of another event.


Thank you!


Best Answer

Hi Javier, 


What you could do is set up a third Leaderboard and event, then place some code in the SCORE1 and SCORE2 events that will take any score posted by either event and post them to Leaderboard 3 for you. So what you can do is set up another event called "SCORE3", then create a new Leaderboard called Leaderboard3 which uses the "SCORE3" number attribute set to "SUM". Next navigate to Cloud Code and open up the events "SCORE1" and "SCORE2", when these are open place the following code into both.


 

//this will get the score passed in from SCORE1 and SCORE2
var score = Spark.getData().score;

//now we'll use SparkRequests to send the SCORE3 event using the scores from SCORE1 & SCORE2
var request = new SparkRequests.LogEventRequest();
request.eventKey = "SCORE3";
request.SCORE3 = score;
var response = request.send();

 

Now when you post to Leaderboards 1 & 2, the scores will automatically be sent to Leaderboard 3 and added together for you. If you navigate to the manage screen and click into Leaderboards you'll see something like below.


On the first Leaderboard I posted a score of 70


On the second Leaderboard I posted a score of 15


My third Leaderboard has the scores from both added together automatically by using the Cloud Code in the Score events for Leaderboard 1 & 2


Hope this helps, if you have any further questions just let me know.


Thanks,

Liam



Answer

Hi Javier, 


What you could do is set up a third Leaderboard and event, then place some code in the SCORE1 and SCORE2 events that will take any score posted by either event and post them to Leaderboard 3 for you. So what you can do is set up another event called "SCORE3", then create a new Leaderboard called Leaderboard3 which uses the "SCORE3" number attribute set to "SUM". Next navigate to Cloud Code and open up the events "SCORE1" and "SCORE2", when these are open place the following code into both.


 

//this will get the score passed in from SCORE1 and SCORE2
var score = Spark.getData().score;

//now we'll use SparkRequests to send the SCORE3 event using the scores from SCORE1 & SCORE2
var request = new SparkRequests.LogEventRequest();
request.eventKey = "SCORE3";
request.SCORE3 = score;
var response = request.send();

 

Now when you post to Leaderboards 1 & 2, the scores will automatically be sent to Leaderboard 3 and added together for you. If you navigate to the manage screen and click into Leaderboards you'll see something like below.


On the first Leaderboard I posted a score of 70


On the second Leaderboard I posted a score of 15


My third Leaderboard has the scores from both added together automatically by using the Cloud Code in the Score events for Leaderboard 1 & 2


Hope this helps, if you have any further questions just let me know.


Thanks,

Liam


Hi Liam,
Thank you very much for the answer, it seems to be exactly what I need. I'll let you know if I have any problems.

 

Login to post a comment