Javascript functions not working in triggers

Jun 01, 2016

I'm building a game for students to practice singular subjects and plural subjects (like "datum" = singular; "data" = plural).

I want to randomly display words and have students select only singular subjects. This means having a bank of strings for Storyline to randomly select and display when the user clicks a button.

I saw many discussions about random number generators, and only one on random words here, but since my word bank is rather large, and there are multiple functions, it would be best to create a separate .js file to store in the "story content" folder. I've used this page to learn how to work with javascript and insert the extra line of code into the "story" file.

My problem is I'm having trouble figuring out what to write in the "Execute Javascript" trigger to make it all work. I've tried the following, whereas "getSubjectChoice" is the function and it returns the variable "subjectChoice": a randomly selected word. 

var subjectChoice = getSubjectChoice();
var player = GetPlayer();
player.SetVar("subjectChoice", subjectChoice);

I've also tried replicating what David Cox did with "Game 1" and "Game 2" in this discussion, but it doesn't work. Which just tells me I'm missing something.

What do I need to enter in to Storyline for it to access the functions and the arrays in my .js file?

Attached is the code.

Thanks for any and all help you can provide.

2 Replies
Dave Cox

Hi Cassandra,

They way I do this is to build call my javascript functions from Articulate. for example, I enter "getGame(Game01);" into the javascript trigger in Articulate to call the javascript function getGame, and pass the string "Game01" to the function. The string Game01 references the name of a variable in my javascript file. That allows me to access a variable object which has data for the selected game. You don't have to pass a variable if you don't need one, it just depends on how you set up your javascript function. 

Once I have the javascript running in the browser, I setup all of the articulate varialbe from my functions. You always need to setup GetPlayer so javascript knows how to access the articulate variables. Say you want to set the value of myVar in articulate. First you have to add that variable in articulate, and set its variable type. For this disccussion, let's assume that it is a text variable. Now let's set the value of myVar to "My Test Value". In your Javascript file the statement "GetPlayer().SetVar("myVar", "My Test Value");" Likewise, you can also pass the value of a javascript variable to that variable, providing it is a text string. (Remember, our articulate variable is a text variable.) Let's pass the contents of myVarStr to articulate: In that case, use the string "GetPlayer().SetVar("myVar", myVarStr);" Notice, since I'm sending the contents of a variable, the name is not quoted. 

In a similar fashion, I can retrieve the contents of an Articulate variable. This statement will retrieve the value of myVar and store it in the javascript variable jsMyVar: var jsMyVar = GetPlayer().GetVar("myVar");" 

Don't forget, your javascript file needs to reside in the story_content folder of your published project, and you have to add the statements in the story.html file to include the javascript file.

I hope that this information helps.

Dave

Christie Pollick

Hi, Cassandra -- Welcome to the community!

In case you were unaware, I thought I might mention that Articulate does not provide support for JS, however, as you can see from the replies you have already received, we have many here in the community who are quite knowledgable and are more than happy to assist!

Here also is our JavaScript Best Practices and Examples if you'd like to take a look, as well. :)

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