Forum Discussion
java script
please;;;
I want to know how can i write java script code to get sum of 5 variables?????????
9 Replies
- DaveCoxCommunity Member
Hi Fatma,
This will do it.
var player = GetPlayer();
var sum = player.GetVar("Variable1") + player.GetVar("Variable2") +
player.GetVar("Variable3");
player.SetVar("Sum", sum);Replace Variable1, Variable2, Variable3 and Sum with your own variable names in Storyline. I only did three, but you can add as many as you want.
- FatmaSayedCommunity Member
Thank you for your help Mr Dave Cox. :)
- WaltHamiltonSuper Hero
FYI:
you can do the same thing with triggers, using whatever action you are using to initiate the javascript.
Adjust variable Sum = 0 when user clicks button
Adjust variable Sum ADD Variable 1 when user clicks button
Adjust variable Sum ADD Variable 2 when user clicks button
Adjust variable Sum ADD Variable 3 when user clicks button, etc.
You just need a trigger for each step (6 triggers) that all fire from the same user action.
The advantage of using triggers is that it is easier for someone who doesn't write JS to do maintenance on the module. Also, triggers function in preview mode, while your module must be published for JS to function.
- FatmaSayedCommunity Member
Thanks for helping Mr/ Walt Hamilton, this is a piece of cake, and i will use it.
- RichardHill-4f3Community Member
If you need more help Ashley Chiasson just made this tutorial this week on adding variables with triggers. It's a nice little video. http://ashleychiasson.com/blog/screencast-adding-values-using-variables-in-articulate-storyline/
- FatmaSayedCommunity Member
Thank you so much MR/Richard Hill. :)
- DaveCoxCommunity Member
Hi Fatma,
Unless there is another reason you want to do this in Javascript, I suggest it would be better to use Walt's method above. Javascript is very powerful, and can let you do a lot of cool stuff. But, it can be difficult to troubleshoot, and will not run in the preview mode. I only add javascript to my projects when I can't figure out any way to do it in Storyline alone.
- FatmaSayedCommunity Member
You are right MR/ Dave Cox, thanks :)
- JamesMartin-49fCommunity Member
I just wanted to note, for anyone who might be diving into JavaScript in Storyline, that Dave Cox's solution, at the top of this thread, works just fine here in 2022 with the latest version of Storyline 360 (v3.61.27106.0). Thanks, Dave, for the help. You steered me in the right direction.
(Now that I've made it work in JS, I'm going to try making it work using Walt's trigger-based solution.)
Related Content
- 7 months ago
- 7 months ago
- 4 months ago