Javascript and Variables

Jan 27, 2014

Hello all!

I am trying to put together a board game quiz in Articulate Storyline using JavaScript.  I using the Javascript random number code to act as the dice in the game.  I was wondering if anyone knew of a JavaScript that would add numbers to a variable.  I will give an example.  

Say you have a number variable set to 0 as default.

You "roll" a 5 and have the JavaScript set the variable to 5.

Next turn you "roll" a 2.  I want the JavaScript to add the 2 to the available variable 5 to make the variable 7.

Is this something you can do in Storyline?  I know the JavaScript is very limited to what it can do.  

Thanks,

Jacob

3 Replies
Jacob Wiese

Hey Michael,

Thanks for the speedy reply!  That interaction is almost exactly what I was looking for.  Everything but the "Counter".  After going through a few JavaScript examples, I was able to come up with something.  It does what your interaction does but with less triggers.  

var randomnumber = Math.floor((Math.random()*3)+1);

var player = GetPlayer();

player.SetVar("Roll",randomnumber);

var roll = player.GetVar ("Roll");

var realroll = player.GetVar ("RealRoll");

var present = parseInt(roll) + parseInt(realroll);

var player = GetPlayer();

player.SetVar("Present", present);

All I needed to do was add in the 3 variables and now I should be good to go!  

To be honest, I didn't even think about looking at he Operator drop down for variable controls.  I may still use them to add in some "You answered wrong, go back one space" option.  Thanks for the idea!

Michael Hinze

If you are trying to add the variables within JS, see here for info on numeric operators: http://www.w3schools.com/js/js_operators.asp .

If you are trying to add the variables that were passed from JS to Storyline, then see here for some info on options: http://community.articulate.com/tutorials/products/building-a-simple-counter.aspx

This discussion is closed. You can start a new discussion or contact Articulate Support.