JavaScript And Storyline: Timesaver

Shares

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!

About the Author olahzsolt@hotmail.com

Leave a Comment:

4 comments
Add Your Reply