Controlling Variables from JavaScript

I often have to use multiple True/False variables to keep track of things. Let’s say they are collectibles. If it’s true, you have it. If it’s false, you don’t. Now, many times I want to easily toggle these. Like pick it up (true) and drop it (false).

Now, imagine that a door’s state depends on these collectibles. Maybe you need at least 3 of these collectibles (3 true variables) for the door to open. But any 3!! Without JavaScript, it would be tons of triggers and conditions.

With JavaScript, a couple of lines:

toggle

LAUNCH EXAMPLE 

DOWNLOAD EXAMPLE

In Storyline, I have variables set up: Var1, Var2, Var3, Var4, Var5. The heart of the JavaScript trick is a function you call:

function toggleVar(item)
{
var play=GetPlayer(); // get the player
var counter=0; // this will count the number of true variables
// set the Var+item (Var1 or Var2 or Var3 etc.) variable to the opposite (! mark makes a true false and a false true)
play.SetVar("Var"+item,!play.GetVar("Var"+item));

and the fact that in JavaScript you make up names on the fly by adding “Var” and “item” together, where item is 1,2,3,4 or 5. One single function can be called from anywhere in StoryLine. All you need to change is the number (item) you send to the function.

Download the example with source files and explore! As always, any questions? Tweet me at @rabbitoreg!

3 Replies to “JavaScript And Storyline: Timesaver”

  1. Kathleen says:

    I wanted to reverse engineer your storyline file but I was not able to get this to work. There is a Lock text variable and a Locked number variable but there is not trigger for the Lock text variable?

  2. […] (Pro Tip: you don’t need to write 40 lines of code like var player = GetPlayer(); p.SetVar(“char1”,gotCharacter[0]); p.SetVar(“char2”,gotCharacter[1]); p.SetVar(“char3”,gotCharacter[2]); etc. You can put them into a loop and generalize it. Here’s an example.) […]

Leave a Reply

Your email address will not be published. Required fields are marked *