A Better Way to Manage Variables

Nov 16, 2017

I've always thought the way Storyline handles variables was too limited - requiring too many variables to handle simple challenges.


So I've written a solution you can use in any of your courses. A little snippet of JavaScript that makes it simple to track lots of issues.


1. Create TWO native Storyline variables –

a. “Variable One” type is TEXT and its initial value is EMPTY ( no value )
b. “Variable Two” type is NUMBER and its initial value is ZERO

2. Let’s say you have FOUR items on the screen and you want to track whether all four have been selected before an action can take place ( let’s say go to the next slide )

a. Normally you would need FOUR variables – one for each item, and then you would test the value of each item to see if they met a requirement before the action would take place
b. That’s a waste of variable power

3. Instead this solution let’s you ADD a single character to a string when an item is actioned so you can determine if ALL items have been actioned using ONE VARIABLE

4. Consider this:

a. You click “item one” and the code “adds” the letter “a” to a string, and sets the value of “Variable One” to “a” ( the value of the string ) AND the value of “Variable Two” to 1 ( the number of characters in the string )
b. You click “item two” and the code “adds” the letter “b” to a string, and sets the value of “Variable One” to “a,b” AND the value of “Variable Two” to 2
c. You click “item three” and the code “adds” the letter “c” to a string, and sets the value of “Variable One” to “a,b,c” AND the value of “Variable Two” to 3
d. You click “item four” and the code “adds” the letter “d” to a string, and sets the value of “Variable One” to “a,b,c,d” AND the value of “Variable Two” to 4

5. No matter how many ADDITIONAL times you click ANY of the items, no more characters are added because they already exist in “Variable One”

6. Now all you have to do is check to see if the value of “Variable Two” equals the number of items ( 4 ). If it does then the action can occur. If it doesn’t then you haven’t clicked all of the items on the screen.

7. There’s a lot more you can do with this simple process, but at least you have this option to work with – and don’t forget – you’re not limited to letters. A number is also a character that can be added to the string – I’d just stay away from special characters.

8. Here’s the javascript code:

//set the value to be added ( must be inside quotes to ensure string assignment )
//when the item is selected/clicked
var thisItem = "a";

var player = GetPlayer();
var result = player.GetVar( "VariableOne" );
var resultArray = result.indexOf(',') == -1 ? result.split('') : result.split(',');
if( resultArray.indexOf(thisItem) == -1 ){

resultArray.push(thisItem);
resultArray.sort();

player.SetVar( " VariableOne", resultArray.join() );
player.SetVar( " VariableTwo", resultArray.length );

}

2 Replies

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