Forum Discussion
GetPlayer GetVar in javascript
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
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.
- ChrisWalshCommunity Member
This is an old thread but I would use the browser developer tools to set a breakpoint on the
ifstatement and probe to see exactly what kind of object you have in theplayervariable at that point.I would expect that if
playeris not a null value (and checked by theifstatement) then it ought to be a legitimate reference to the player and therefore theGetVarfunction 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
playerobject is not in a fully initialised state.- AriffKhalidCommunity Member
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.
Old or not, we always appreciate the updated info and ideas!
- ChrisWalshCommunity Member
Hi Ariff,
Can you provide more info about what you see on the breakpoint. Specifically:
- On the line after
var player = GetPlayer();, what is the value of theplayervariable? Is it null? or is it an object? - If
playeris not null, does it contain a function calledGetPlayer()? You can test this by typing the following in the console window:player.GetVar(note that no brackets are present). - If the
player.GetVarfunction exists, what is the result of the call toplayer.GetVar("var_name_goes_here")? (don't forget to changevar_name_goes_herewith 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.
- On the line after
- DarrylPalaub492Community Member
Any updates on this?
- Jürgen_Schoene_Community Member
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