plane icon Welcome to Microsoft Flight Simulator’s SDK Q&A Platform!

You have questions regarding the SDK? DevMode Tools? SimConnect? You would like to submit an idea for future improvements, seek help or exchange knowledge? You’re in the right place.

In the upcoming flighting, we've changed the behaviour of the content.xml file. If your addon uses this file, please read this article!

Please take a moment to read the platform’s guidelines before you get started!


question

CaptMatto avatar image
CaptMatto asked eidar commented

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

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="/SCSS/common.css" />
    <link rel="stylesheet" href="bonanza-hangar.css" />

    <script type="text/javascript" src="/JS/coherent.js"></script>
    <script type="text/javascript" src="/JS/common.js"></script>
    <script type="text/javascript" src="/JS/dataStorage.js"></script>
    <script type="text/javascript" src="/JS/buttons.js"></script>
    <script type="text/javascript" src="/JS/simvar.js"></script>
    <script type="text/javascript" src="/JS/Services/ToolBarPanels.js"></script>

    <script type="text/javascript" src="/Pages/VCockpit/Instruments/Shared/Utils/DataReadManager.js"></script>
    <script type="text/javascript" src="/Pages/VCockpit/Instruments/Shared/BaseInstrument.js"></script>

    <link rel="import" href="/templates/ingameUi/ingameUi.html" />
    <link rel="import" href="/templates/ingameUiHeader/ingameUiHeader.html" />
    <link rel="import" href="/templates/checkbox/checkbox.html" />
    <link rel="import" href="/templates/NewPushButton/NewPushButton.html" />
    <link rel="import" href="/templates/ToggleButton/ToggleButton.html" />

    <script type="text/javascript" src="bonanza-hangar.js"></script>
</head>

<body id="" class="">
    <bonanza-hangar>
        <ingame-ui id="bonanza_frame" panel-id="G36_Panel" class="ingameUiFrame panelInvisible" title="Bonanza Hangar" content-fit="true" min-width="33" min-height="35">
            <section id="MainDisplay">

            </section>

            <section id="Footer" class="">

            </section>
        </ingame-ui>
    </bonanza-hangar>
</body>
</html>

And this is the code in the .js

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

  }
  connectedCallback() {
    console.log('test');
    let lat = SimVar.GetSimVarValue("PLANE LATITUDE", "degree latitude");
    console.log(lat);
  }

}
window.customElements.define("bonanza-hangar", BonanzaHangar);
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.

1296-1636393826475.png

This is what's being shown in simvar.js

screenshot-2021-11-09-081259.png


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

bughelphtml
6 comments
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

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.

1636311947772.png

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

0 Likes 0 ·

Looks mostly alright to me.

Does it work when you try to make the call from the Coherent Debugger console?

1 Like 1 ·

Thanks so much for replying!

This is what I get when making the call in the console:

1636382905711.png

If I use Let or Var :

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

I get an undefined message, but if I just run

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

I get the latitude....


0 Likes 0 ·
1636382905711.png (18.0 KiB)
Show more comments
N6722C avatar image
N6722C answered CaptMatto commented

I cannot see why

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

let position = new LatLong();
position.lat = SimVar.GetSimVarValue("PLANE LATITUDE", "degrees");
position.long = SimVar.GetSimVarValue("PLANE LATITUDE", "degrees");
1 comment
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

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.

0 Likes 0 ·
RoscoHead avatar image
RoscoHead answered dga edited

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.

2 comments
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

@RoscoHead, thank you!

So success, if you add the code into a timer it supplies the simvar.

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


Could this be a bug??? My understanding of

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?

0 Likes 0 ·

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.

1 Like 1 ·
CaptMatto avatar image
CaptMatto answered

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

10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

Mr_LiamT avatar image
Mr_LiamT answered eidar commented

You can use this:

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

The code only gets executed if the simvar has loaded

1 comment
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

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()

1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 5 attachments (including images) can be used with a maximum of 19.1 MiB each and 23.8 MiB total.