ReferenceError: Can't find variable: simvar

I’ve created a custom in game panel which is loading ok. I’m having trouble
however accessing simvars via the in game panel. I’m getting ReferenceError:
Can’t find variable: simvar error in the coherent debugger. This is the code
I’m using to load the HTML

      1.   2.   3.   10.      type="text/javascript" src="/JS/common.js">  41. 

And this is the code in the .js

      1. class BonanzaHangar extends HTMLElement {
  2.   constructor() {
  3.     super();
  4.  
  5.   }
  6.   connectedCallback() {
  7.     console.log('test');
  8.     let lat = SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude");
  9.     console.log(lat);
  10.   }
  11.  
  12. }
  13. window.customElements.define("bonanza-hangar", BonanzaHangar);
  14. checkAutoload();

Can anyone see where I am going wrong? It feels like I am not referencing
Simvar correctly but been scratching my head all day. This is the error from
the console.

This is what’s
being shown in simvar.js
UPDATE: Thank you to you all for posting suggestions. This is what has
been tried so far and a possible solution by RoscoHead

  • Use console to check can read simvars - works
  • Try to add an addEventListener to wait until all js loaded - Doesn’t work (listener runs but simvar doesn’t work, tried in a connectedCallback and outside the class.)
  • Checked loading order of .js files as per the Asobo panels, this mirrors those.
  • Putting the simvar code into a window.setInterval waiting for 5secs works

There must be something that simvar.GetSimVarValue relies upon that isn’t
loading/initialising’ (for at the most 5secs) after the flight has loaded.
I’ve tried putting the code into onFlightStart() but this does not run at all.
Is there a list of functions available to the HTMLElement and when they run?
Does anyone have any insight into the timeline where the panel loads into the
sim? I thought it only loads when you click the icon? (see @RoscoHead’s issue
with trying to preload) It seems like waiting a few seconds then opening the
panel also doesn’t work so there is some loading weirdness going on here. If
anyone is interested this is a link to the panel code in its entirety in a
.zip. <https://yachtmanagercouk-
my.sharepoint.com/:u:/g/personal/matt_yachtmanager_co_uk/EWnl1akTrmBFmHwj3IfwaxcBccFXbqHj6UR-
XdCFwQlGdw?e=DGa7Sb>

Sadly not getting very far with getting this working. I’ve checked and double
checked the code behind the custom panel, recompiled the spb etc. The panel
itself loads correctly but I can’t seem to be able to access any simvar. I
must be missing something in the panel html or javascript but really can’t see
where. This is the error i’m getting.

This is the completed code to
where I am at the moment
<https://github.com/50North4West/msfs_g36_project/tree/development/captmatto-
panel-bonanza>

Looks mostly alright to me. Does it work when you try to make the call from
the Coherent Debugger console?

Thanks so much for replying! This is what I get when making the call in the
console: If I use Let or Var :

      1. let lat = SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude");

I get an undefined message, but if I just run

      1. SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude"); 

I get the latitude…

Well, since you assigned it to a variable in the console, the undefined looks
correct as a response. You could do:

window.testLat = SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude");
window.testLat

In the console, and that should give you your lat back. My guess is that in
this case above, the BonanzaElement HTMLElement is actually mounted to the DOM
before the script elements are (or something like that), and thus the JS is
not yet fully loaded when you get to connectedCallback. You may need to add an
event listener to something like DOMContentLoaded to ensure all the JS is
loaded before trying to set up your update cycle. -Matt

Ah that makes sense!

Thank you so much @mattnischan & @dga - sometimes a fresh pair of eyes! :slight_smile:

Bummer I thought we were getting somewhere I’ve tried the following but still
get the same error message.

class BonanzaHangar extends HTMLElement {
  constructor() {
    super();
  }
  connectedCallback() {

  }
}
window.customElements.define("bonanza-hangar", BonanzaHangar);
checkAutoload();

window.addEventListener('load', (event) => {
  console.log('page is fully loaded');
  let test = SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude");
  console.log(test);
});

window.addEventListener('DOMContentLoaded', (event) => {
  console.log('DOM is fully loaded');
  let test = SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude");
  console.log(test);
});

I cannot see why

      1. let lat = SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude");

would not work correctly (it s used in so many MSFS plane’s JS files) Maybe
try

      1. let position = new LatLong();
  2. position.lat = SimVar.GetSimVarValue("PLANE LATITUDE", "degrees");
  3. position.long = SimVar.GetSimVarValue("PLANE LATITUDE", "degrees");

Hi @N6722C, thank you. Sadly this doesn’t work. I feel I am missing something
obvious, I’ve tried to look at loading order, and looked at the html/js
against the existing panel code.

I don’t currently have access to my PC to check, but I remember I was calling
GetSimVarValue in a loop, and it would get this error the first couple of
times through the loop. I couldn’t get rid of it, so just programmed to ignore
any results until it stops getting the error. I recall I had the same issue
with global vars like Sim rate and used the same workaround.

@RoscoHead, thank you! So success, if you add the code into a timer it
supplies the simvar.

      1. var timerMilSecs = 5000;
  2. var timer = window.setInterval(runme, timerMilSecs);
  3. function runme() {
  4.   console.log(SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude"));
  5. }

Could this be a bug??? My understanding of

      1. addEventListener('load'...

Is that this fires once everything has loaded, js etc. There is something else
deeper in the sim that is not initialising or allowing us to read simvars
until a few seconds into the loading of the flight. It looks like we can’t use
OnFLightStart(). I wonder if there is another function other than
connectedCallback that we can use?

Hm, from my experience I thought connectedCallback should be late enough. But
there indeed seems to be a timing issue in your case. Possible that the flow
is different outside of BaseInstrument.

Interestingly if I delay the panel js for just 1 second it all works. There
has got to be something strange going on here.

You can use this:

      1. if (typeof simvar !== 'undefined') {
  2.     /* Code that uses Simvar.GetSimVarValue() */
  3. }

The code only gets executed if the simvar has loaded

Hello, Calling SimVar in the connectedCallback is too early and it might not
have been loaded yet. Also, a good way to know it SimVar is loaded is to use
SimVar.IsReady()