Javascript help NEEDED: Populating an array based on variable values.

Let's assume I have 5 variables with a numeric value (1 through 5) and 5 corresponding True/False variables.
Can I use Javascript to create an array using the numeric values based on the status of the corresponding variable?
In other words, if:
NV1 = 1 / TF1 = T
NV2 = 2 / TF2 = F
NV3 = 3 / TF3 = F
NV4 = 4 / TF4 = T
NV5 = 5 / TF5 = T
I would want my array in JS to be 1, 4, 5 and if my T/F variables changed...
NV1 = 1 / TF1 = F
NV2 = 2 / TF2 = T
NV3 = 3 / TF3 = T
NV4 = 4 / TF4 = F
NV5 = 5 / TF5 = T
I would want my Javascript array to now be 2, 3, 5

Does this make sense? Can it be done? Would I even need the numeric value variables or could I just use the T/F ones? 

Help me Javascript experts, you're my only hope!

6 Replies
Alexandros Anoyatis

Hi Owen,

Yes you can just use the T/F ones, and place listeners on all 5 t/f variables to trigger the JS snippet that would rewrite the 'array' var.

Having said that, there's no need to transform your text var to an array, but rather just organize each "True" check to that text var (unless you want to get data checks back from that variable, in which case an array is the way to go).

Hope this helps,
Alex

OWEN HOLT

I will need the array as I want to pull a random number back out of just the array values.

Something like
var array = [2,3,5];
var num = Math.floor(Math.random() * array.length);

What I need help with is creating the array with the specific numeric values based on the T/F variables or the associated numeric ones. I am not a Javascript expert, so I really have no idea where to start with the code to create the array.

 

OWEN HOLT

Thanks Alexandros for the inspiration, but I think you are still speaking at a level too advanced for me. :-)

I was able to solve this using something like the following: 

1) Call to the player
var player=GetPlayer();

2) Pull my SL variables into JS
var nv1=player.GetVar("V1");
var nv2=player.GetVar("V2");
var nv3=player.GetVar("V3");
var nv4=player.GetVar("V4");

3) Group them into an aray
var values1 = [nv1, nv2, nv3, nv4];

4) filter out any variables with a "0" value (variables changed to 0 do to some prior action in SL)
var filtered1 = values1.filter(function(x) {return x>0;});

5) Pull a random variable from the filtered array
var rand = filtered1[Math.floor(Math.random() * filtered1.length)];

6) Send the result back to StoryLine
player.SetVar("ShowLayer",rand);

The end result, combined with the triggers in the project I am working on, is:
a random, non-repeating result (value stored as a variable) from an array (that decreases in size) until all values from the original array are randomly shown (until their corresponding variable is changed to "0").