Forum Discussion
JavaScript Help - Adding 5 variables to 1 for a Total Score in Storyline
Does anyone know why my JavaScript trigger isn't working in Storyline? I have 5 scenarios, each with a sectional score (ScorePessimist, ScoreExpert, etc.). I also want to show a Total Score (ScoreTotal) as learners can self-select the order in which they tackle the scenarios from a Menu. The Total Score should be the sum of all of the sectional scores.
Attempt 1) This worked and updated the ScoreTotal variable in Storyline to 10.
var player = GetPlayer();
var ScoreTotal1 = 10;
player.SetVar("ScoreTotal", ScoreTotal1);
Attempt 2) This did nothing, Total Score stayed at 0.
var player = GetPlayer();
var ScorePessimist1 = player.getVar("ScorePessimist");
var ScoreExpert1 = player.getVar("ScoreExpert");
var ScoreIndecisive = player.getVar("ScoreIndecisive");
var ScoreImpatient = player.getVar("ScoreImpatient");
var ScoreAggressor = player.getVar("ScoreAggressor");
var ScoreTotal = ScorePessimist + ScoreExpert + ScoreIndecisive + ScoreImpatient + ScoreAggressor;
player.SetVar("ScoreTotal", ScoreTotal);
Attempt 3) This did nothing, Total Score stayed at 0.
var player = GetPlayer();
var ScoreTotal1 = player.getVar("ScorePessimist") + player.getVar("ScoreExpert") + player.getVar("ScoreIndecisive") + player.getVar("ScoreImpatient") + player.getVar("ScoreAggressor");
player.SetVar("ScoreTotal", ScoreTotal1);
Each of my sectional scores (for example, ScorePessimist) are number variables adjusted by 10 or -10 each slide.
I'm not very familiar with JavaScript so any help would be appreciated!
Best,
Sam
- JudyNolletSuper Hero
You don't need JavaScript to add the value of variables. Just use a set of triggers like these:
- The first trigger, which sets the ScoreTotal to 0, is used to ensure that the scores don't get added more than once (for example, if the user revisits the slide).
- Instead of "when the timeline starts," the triggers could be attached to a button.
- In the Trigger Wizard, any item with a dashed underline can be edited. That's how to access the math operators:
- SamKauffman-3fdCommunity Member
Thank you, Judy!
Kind regards,
Sam
- MathNotermans-9Community Member
And if for some reason you need ( or want ) to use Javascript... the trick needed is converting the received variable to a numeric...as Storyline variables always will be strings when grabbed by Javascript.
So use parseInt( ) on a grabbed variable for calculation...
eg:var scoreP = player.getVar("ScorePessimist");
var scoreE = player.getVar("ScoreExpert");
var scoreI = player.getVar("ScoreIndecisive");
var scoreIm = player.getVar("ScoreImpatient");
var scoreA = player.getVar("ScoreAggressor");
var ScoreTotal1 = parseInt(scoreP)+parseInt(scoreE)+parseInt(scoreI)+parseInt(scoreIm)+parseInt(scoreA);
player.SetVar("ScoreTotal", ScoreTotal1);
- SamKauffman-3fdCommunity Member
Hey Math,
Thank you for this! This will help me on this and future projects.
Kind regards,
Sam