Forum Discussion
Ensuring a Random Number is not Re-picked
Alternative SL360 file that adds some code to make setting the range easier.
On slide one, designate the high end of your range. JavaScript will create the array, convert it to a string, and send it to your variable so you don't have to type in 1,2,3,4,..... as the default value for the variable. Note, the input is optional as you could hard code the upper limit if it is known and not going to be variable.
Here is the additional JS on slide one:
// get the SL player
var player=GetPlayer();
//Optional: only use this if you are not hard coding the upper range limit.
//The value is supplied by a numeric entry box tied to a SL variable called HighRange
var highLimit=player.GetVar("HighRange");
//Create and populate the array
//If you are hard coding the upper limit, replace "highLimit" with the numeric value of your upper limit.
var textArray = [];
for (var i = 1; i <= highLimit; i++) {
textArray.push(i);
};
//Get the length of the array
var itemsLeft = textArray.length;
//Convert the array to a string and send the variable values to SL
textArray=textArray.map(String).toString();
player.SetVar("Text_Array", textArray);
player.SetVar("Items_Left", itemsLeft);
See it working here.
SL2 version provided below.
Owen - I have a knowledge check dice game with only 6 numbers. Currently I am using the Random number variable, but of course the learner roll 1-5, answer questions 1-5 and have to roll 10 more times to get question #6.
If I use your java - do I take out the random number variable? I am also not clear how, when the array picks a number I plug that number into a text variable (%diceroll%!) so that I can trigger a jump to a slide/question.
Thanks.
- OwenHolt7 years agoSuper Hero
Correct, you would not need to use StoryLine's random number if you are using JavaScript.
Once you generate a random number using JS, you send it back to SL using the setvariable code.
In this example -> player.SetVar("Text_Array", textArray);
player.SetVar is the command.
"TextArray" is the name of the StoryLine variable being used.
textArray is the JS variable that has the value you are sending back to StoryLine.