SL and Java script

May 21, 2012

Hi, I am not familiar with Java script but will learn if it will help,

I have a navigation slide, the concept will be a game of cards, depending on the sequence of cards dealt will trigger an independant scene in the module.

I could just set a number of plays on the cards and then trigger each scene in a sequence but this is predictable, if someone revisits the module.

I then saw the execute java script trigger option. From looking on the net there is a java function var randomnumber=Math.floor(Math.random()*11)    this script generates a random number from 0~10

My question, how do I pass the variable "randomnumber" back to SL or can Java directly interact with a predifined SL variable.

If this script can work then I can use a random number to deliver one of "n" predefined sequences of cards, this will make the order of the scenes different for each learner.

Can anyone assist with the syntax of passing variables back and forward?

7 Replies
Colin Harm

To answer my own question after much searching and deconstructing.

The script I used is

var number=Math.floor(Math.random()*11);
GetPlayer().SetVar("RandNum",number);

Line one generates a random number between 0~10 and places it in a variable called number

Line two passes the variable number to the variable "RandNum" which is a variable I defined in SL

Probably could have been done in one line but this seems intuitive.

Now I will create 10 card sequences that will be linked to the variable, five of which will launch a scene.

So you may ask what about the other five possibilities? you never win all the time with cards.

Kai R

Colin Harm said:

var number=Math.floor(Math.random()*11);

GetPlayer().SetVar("RandNum",number);

Line one generates a random number between 0~10 and places it in a variable called number

Line two passes the variable number to the variable "RandNum" which is a variable I defined in SL

Probably could have been done in one line but this seems intuitive.

Now I will create 10 card sequences that will be linked to the variable, five of which will launch a scene.

So you may ask what about the other five possibilities? you never win all the time with cards.


Hey Colin The JS code looks good, BUT did you get it to work? Other threads say JavaScript support is lacking and I tried your script without success. The initial value of RandNum (0) never changed, no matter how many times I jumped back to the slide.

Any further insight would be greatly appreciated, as I'm both in bad need of random numbers AND of further JS integration.

Gerry Palmer

Just as a heads up, there was a recent change to the Google Chrome flash plugin that prevents Flash from communicating with javascript when run from your local machine. You can see the details of the bug located below:

http://code.google.com/p/chromium/issues/detail?id=137734

Javascript actions should work properly when run locally in other browsers such as Firefox, Internet Explorer, or Safari.  Javascript actions should work in all browsers when run from a web server.

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