GetPlayer GetVar in javascript

Jan 27, 2016

Hi,

I'm testing some of the javascript features. Using a trigger and calling functions in external javascript file (e.g. "user.js") is working correctly. From within the function I can get the player and get or set vars.

In a different scenario, I'd like to control (get or set) variables without an explicit call associated with a trigger.

For instance, in user.js I have a pure javascript "document ready" handler, and a function the simply gets storyline player and fetch a variable.

docReady(function() {
var player = GetPlayer();
if (player) {
var page = player.GetVar("maxPage");
}
});

The error is Uncaught TypeError: player.GetVar is not a function

this not supposed to work or am I doing something wrong?

thanks

 

7 Replies
Ashley Terwilliger-Pollard

Hi Cyber Tech,

We can't offer support for specific Javascript elements, so hopefully someone in the community is able to weigh in here. In the meantime you may also want to review the information here in regards to the Javascript Best Practices. Best of luck with your project and I hope someone in the community is able to assist. 

Chris Walsh

This is an old thread but I would use the browser developer tools to set a breakpoint on the if statement and probe to see exactly what kind of object you have in the player variable at that point.

I would expect that if player is not a null value (and checked by the if statement) then it ought to be a legitimate reference to the player and therefore the GetVar function should exist (but you can check this when the breakpoint is hit).

The other thing to do is check the console for any other errors that may have occurred prior to the breakpoint that might mean that the player object is not in a fully initialised state.

Ariff Khalid
Chris Walsh

This is an old thread but I would use the browser developer tools to set a breakpoint on the if statement and probe to see exactly what kind of object you have in the player variable at that point.

I would expect that if player is not a null value (and checked by the if statement) then it ought to be a legitimate reference to the player and therefore the GetVar function should exist (but you can check this when the breakpoint is hit).

The other thing to do is check the console for any other errors that may have occurred prior to the breakpoint that might mean that the player object is not in a fully initialised state.

hi chris,

stumbled across this because i'm facing the same issue as original poster. the breakpoint doesn't seem to be showing anything specific and all im trying to achieve is to trigger the function.

Chris Walsh

Hi Ariff,

Can you provide more info about what you see on the breakpoint.  Specifically:

  1. On the line after var player = GetPlayer();, what is the value of the player variable?  Is it null? or is it an object?
  2. If player is not null, does it contain a function called GetPlayer()?  You can test this by typing the following in the console window: player.GetVar (note that no brackets are present).  
  3. If the player.GetVar function exists, what is the result of the call to player.GetVar("var_name_goes_here")? (don't forget to change var_name_goes_here with your variable name and also be aware that the string value is CaSe SenSiTiVe!!!

Drop feedback and I might be able to help more.  Good luck.

Jürgen Schoenemeyer

copy this function in your user.js

document.addEventListener("DOMContentLoaded", function(_event) {
function GetVariable( inVarName ){
if (typeof GetPlayer === "function") {
var value = GetPlayer().GetVar( inVarName );

console.info( "var " + inVarName + ":", value );

} else {
window.setTimeout( function(){ // wait 1 second, try again
GetVariable( inVarName )
}, 1000 );
}
}

GetVariable( "maxPage" );
});

Jürgen