Javascript in Storyline: 2 Examples

Nov 29, 2017

Howdy all,

First time posting, but I just thought I'd share two snippets of code I'd been working on.

1. Keyword Search:

I wanted to be able to search a user input field for certain words and, based on whether the words were hit or not, adjust a variable in SL. It's currently used to ask users about their course goals and then highlight any that match what we might be covering in the course.

You'll want to set some of the variables beforehand in SL.

function keywordSearch () {
var player = GetPlayer(),
userGoals = player.GetVar("userGoals").toUpperCase();
array1 = ['FUNDRAISING', 'RAISING MONEY', 'DEVELOPMENT'],
array2 = ['EATING CAKE', 'EATING PIE', 'EATING COBBLER'],
array3 = ['WAILING', 'SOBBING', 'CRYING'];


for (var i = 0; i < array1.length; i++) {

if (userGoals.includes(array1[i])) {
player.SetVar('goal1', "Fundraising");
}
}

for (var i = 0; i < array2.length; i++) {

if (userGoals.includes(array2[i])) {
player.SetVar('goal2', "Consuming baked goods");
}
}

for (var i = 0; i < array3.length; i++) {

if (userGoals.includes(array3[i])) {
player.SetVar('goal3', "Feeling sorry about your life");
}
}

}

keywordSearch();

 

2. Another thing I made was a word counter (to limit the number of words within a text entry field). So long as you have the counter adjust a variable, then you can use that to trigger other sorts of actions (a warning message, change of state, etc.)

P.S. I cheated... because counting spaces is much easier than actually counting words.

var player = GetPlayer(),
spaceCount = 0;

for (var i = 0, x = player.GetVar("userGoals"); i < x.length; i++) {
if (x[i] === ' ') {
spaceCount++;
}
}

if (spaceCount > 19) {
Number(player.SetVar("userGoalError", 1));
Number(player.SetVar("numWords", (spaceCount - 19)));
} else {
Number(player.SetVar("userGoalError", 0));
Number(player.SetVar("numWords", 0))
}

12 Replies
OWEN HOLT

This is NOT Kody's file.  However, I created this following his instructions.  Use the keywords and under 10 words to see what happens. Then increase your word count to more than 10 and try again.  This will show you both sets of code in action that Kody created.

See it in action (SL360) here.

SL2 file attached below.

Lori Greenberg

The challenge we're facing is that "includes" isn't a javascript statement that works in Internet Explorer. Apparently you can use "indexOf" but we haven't worked out exactly how yet... anyone up for trying it out?

From StackExchange: "String.prototype.includes is, as you write, not supported in Internet Explorer (or Opera)

Instead you can use String.prototype.indexOf. #indexOf returns the index of the first character of the substring if it is in the string, otherwise it returns -1. (Much like the Array equivalent)"

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