Javascript & Storyline - What does var player = GetPlayer() function do?

May 25, 2018

Hello. In a few months, I will be giving a brief presentation regarding ways to use JavaScript in Storyline. Can someone please assist with answering the following question? In the JavaScript + Storyline articles I've found, the action: var player = GetPlayer(); is always used somewhere in the code. Can someone please explain what this action does and when it should be placed within the JavaScript code?

Thanks!

9 Replies
Zsolt Olah

var player=GetPlayer();

"player" becomes a variable in JavaScript that you can use anywhere in your code. Think of it is the bridge between JavaScript and Storyline. JavaScript doesn't see any Storyline variables that you create in the application, and Storyline does not see any JavaScript variables. 

What you can use the player for is to get and set Storyline variables in JavaScript.

for example, the new jsScore JavaScript variable retrieves the value of StorylineScore variable from Storyline.

var jsScore = player.GetVar("StorylineScore");

 

Bridget O'Dell

Hi Zsolt,

Thanks for your response.

While I understand that "var player" creates a JavaScript variable called "player". I am hoping for a bit more clarity on what the "=GetPlayer()" function does.

I can understand that you need to include the "var player=GetPlayer();" function before you can use the player.SetVar and player.GetVar functions, but aside from that I don't understand what it does. Can you please provide additional clarification?

Thank you!

Zsolt Olah

It doesn't do much else than setting and reading variables for now (at least what's documented and supported). It would be great if there was support to handle states directly (setting a state of an object in storyline directly), or branching, etc. But today it's only about variables.

OWEN HOLT

At some point in the distant past, the good folks at Articulate opted to support JavaScript.  To facilitate this, they created a JavaScript OBJECT called "player".  Think of an object as a sort of filing system made of labels and some associated content that goes with that label.  You might already be familiar with the JS Date Object, so I will use it as an example.

With the date object, you might see code like the following:
var today = new Date(); //This gets the predefined date object and puts it in a variable so you can use it.
var dd = today.getDate(); //this calls the date object stored in your variable and tells it to get the data stored under the label "Date" which happens to be the calendar day as a number (1, 2, 3, etc.).
var yyyy = today.getFullYear(); //this calls the date object stored in your variable and tells it to get the data stored under the label "FullYear" which happens to be the year as a 4 digit number.
etc.  The date object has many many things in it.

The important thing to understand is that anything can be labeled and stored in an object including other objects, functions or code.

So, back to the StoryLine player object.
var player = GetPlayer(); //stores the predefined player object in a variable so you can use it.
player.GetVar("Variable Name"); //executes some proprietary code created by Articyulate & stored in the player object with the label of "GetVar". This code looks for a variable in your StoryLine file that matches the variable name argument you provided and pulls its value into JavaScript for you to do something with it.
player.SetVar("SL Variable", JS Variable); //executes some proprietary code stored in the player object with the label of "SetVar". This code looks for a variable in your StoryLine file that matches the variable name argument you provided and gives it a new value based on the JavaScript variable argument you have assigned.

Does this help?

Steve Flowers

Setting var player just creates a shortcut to the player methods within the player object. You could set it to var p=GetPlayer(); or another variable of your choosing.

This also works:
GetPlayer().SetVar("testVar","Var Value to Set");

Which is equivalent to:
var p=GetPlayer();
p.SetVar("testVar","Var Value to Set");