20 Replies
OWEN HOLT

Are you open to using JavaScript? Do you always want it rounded DOWN? 

If so: 
var player = GetPlayer(); //locates the SL player and stores player object in a variable
var targetNumber = player.GetVar("your storyline variable's name"); // get your number from SL
targetNumber = Math.floor(targetNumber); //rounds down to whole number
player.SetVar("your storyline variable's name", targetNumber); //sends the rounded number back to your SL variable.

If you want it rounded up or down (basic math rules of rounding):
var player = GetPlayer(); //locates the SL player and stores player object in a variable
var targetNumber = player.GetVar("your storyline variable's name"); // get your number from SL
var baseNumber = Math.floor(targetNumber); //rounds down to whole number
var upOrdown = targetNumber - baseNumber //used to determine if next whole number should be used
if (upOrdown >= .5) {
targetNumber = baseNumber +1
}; //adds 1 to the number you rounded down instead of up if the decimal is greater than or equal to 5
player.SetVar("your storyline variable's name", targetNumber); //sends the rounded number back to your SL variable

OWEN HOLT

Sorry about that, left off a critical else statement.
var player = GetPlayer();
var targetNumber = player.GetVar("MyNumber");
var baseNumber = Math.floor(targetNumber);
var upOrdown = targetNumber - baseNumber
if (upOrdown >= .5) {
    targetNumber = baseNumber +1
    } else{
    targetNumber = baseNumber
    };
player.SetVar("MyNumber", targetNumber);

 

OWEN HOLT

Simplified code using the Math.round function. Replace MyNumber in the code below with the name of your SL variable. This will round up or down to the nearest integer.

var player = GetPlayer();
var targetNumber = player.GetVar("MyNumber");
targetNumber = Math.round(targetNumber);
player.SetVar("MyNumber", targetNumber);

Megan Yeo

Hi Owen

I have re-tried again and turned out that it works. I missed out certain parameters so apologies for my error.

However, I would like my number to round off slightly differently though. For example, the number is 88.88%. Using the above javascript you have provided, the number rounded off to 89%. But I would like the number to round off to 90%. If the number is 66.66%, I would like the number to round off to 70%. Can you please guide me as how to do so?

Thanks again for your help!

OWEN HOLT

I haven't tested, but something like this should work for rounding to the nearest 10.

var player = GetPlayer();

var targetNumber = player.GetVar("MyNumber"); 
targetNumber = targetNumber/10;
targetNumber = Math.round(targetNumber); 
targetNumber = targetNumber*10;

player.SetVar("MyNumber", targetNumber);

Masoud Rabiey

Hi, how can I write a javascript code that creates random numbers from one to ten so that by clicking on a button, this JavaScript command does not repeat the code that it generates, for example, if the first clicks 3 were shown, the next  clicks would not be 3 displayed. .Thanks

OWEN HOLT

You will need a variable for each possible number that you are going to choose from with a default value of 1. When they are selected randomly, you will need to change the value of the appropriate variable to 0. You will use these variables to create an array and then filter the array to remove the 0 values. This will then let you select a random number from the filtered (unselected) array. 
You can read more about it and see it in action in this post: Trivia Game - Using Variables and JavaScript to remove answered questions from the deck.

OWEN HOLT

No problem.  You might check out the simplified code/process I came up with for generating a non-repeating random number from a range of numbers here: Link

It uses fewer StoryLine variables and lets JavaScript do more of the "heavy lifting".   This allows you to use a much larger range of numbers without requiring a large number of variables. In fact, it will only ever require 3 of them, regardless of how large your range is.