• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

MSFS20 Storing time in a local variable

Messages
531
Country
france
Hello experts,

For specific reasons, I would like to store in a local variable (L: var) the exact time at which the aircraft leaves the ground during take off.
I use the ABSOLUTE TIME sim var for this, that I read like this: SimVar.GetSimVarValue("E:ABSOLUTE TIME", "seconds");
If I display this value in the console, it shows it works perfect, it changes as expected when time goes by...

When the aircraft leaves the ground, I do something like this:

let time = SimVar.GetSimVarValue("E:ABSOLUTE TIME", "seconds");
SimVar.SetSimVarValue("L:MY_TAKEOFF_TIME", "seconds", time);

The strange thing is that the local variable named "MY_TAKEOFF_TIME" does not reflect the value of time as expected. This should be very simple but it does not work as expected...
I suspect it is a problem of value type or unit, I tried using "number" instead of "seconds" but it does not work any better.

Any idea?

Thank you,
Eric
 
Hi Eric

Although this is in XML it might help ,

Specifically ,

Code:
        <Select>
            <Value>(L:DistanceTargetPos,nmiles) 6 &lt; (L:DistanceTargetPos,nmiles) 5.8 &gt; and
                      if{ 1 (&gt;L:TokenDistance,enum) }
                   (L:TokenDistance,enum) 1 ==
                         if{ (P:Absolute time, seconds) (&gt;L:Reminder Text START, seconds)
                           0 (&gt;L:TokenDistance,enum) }
            </Value>
        </Select>
Where instead of the distance you would use on or off ground condition.

Cheers
Karol
 
Last edited:
I have hard time understanding XML, but still, I can understand that according to the if condition, the value of the Absolute Time variable is set into the Reminder Text START local variable, using seconds for both variables.
If I understood correctly, this is exactly what I do as well, using JavaScript instead of XML. To be more accurate, this XML code is equivalent to this:

SimVar.SetSimVarValue("L:MY_TAKEOFF_TIME", "seconds", SimVar.GetSimVarValue("E:ABSOLUTE TIME", "seconds"));
Or using the same variables as your example, it would be:
SimVar.SetSimVarValue("L:Reminder Text START", "seconds", SimVar.GetSimVarValue("E:ABSOLUTE TIME", "seconds"));

And it doesn't work... It works as if my local variable was an approximation of the value of ABSOLUTE TIME. It is not completely inconsistent, but the values are not equal as they should be.
 
I'm not familiar with JavaScript , however following observations might be worth mentioning ,
{1} I presume that "SimVar.GetSimVarValue" would come first , then " SimVar.SetSimVarValue" , so you would be getting the current active time value ,
then setting an instantaneous frozen time value to the L Variable .
{2} In your last reply the first variable did not have the closing bracket , but at the very end it had a double closing bracket , possibly that could cause problems.

Possible code format
Code:
 SimVar.GetSimVarValue("E:ABSOLUTE TIME", "seconds"); SimVar.SetSimVarValue("L:MY_TAKEOFF_TIME", "seconds");

One other avenue that might need checking is the trigger that causes the setting of the L Variable value might be acting or
triggering the setting more than once , what you are after is capturing a single instantaneous value .

Cheers
Karol
 
1) You are right, in the way I wrote it SimVar.GetSimVarValue comes first, then SimVar.SetSimVarValue comes after. As you guessed correctly, I want to use this to store the instant at which the aircraft left the ground during takeoff, an instantaneous frozen time value at this specific instant.
2) Don't pay too much attention to the syntax. There is no error here, the syntax just means: take the value of the sim variable named "ABSOLUTE TIME" and put it in the value of the local variable named "MY_TAKEOFF_TIME".

The problem I have is a wrong value in the "MY_TAKEOFF_TIME", but it is not completely wrong, it is not 0. It has a value that is close to the real value of "ABSOLUTE TIME", but it is not equal.
To give you more details, here is what I tried:

let time = SimVar.GetSimVarValue("E:ABSOLUTE TIME", "seconds");
console.log("Current time is " + time);
SimVar.SetSimVarValue("L:MY_TAKEOFF_TIME", "seconds", time);

The second line I added here consists in printing the actual time value that I got from the ABSOLUTE TIME sim var, and I can check it is valid, incremented on a regular basis (18 times per second I think, which means increments of 0.55s).
Then I set this value to "MY_TAKEOFF_TIME" and when I see the value of this variable, it is different from what was printed in the console.
Very strange...
 
Eric: I think you're running into the old problem of expecting the sim to react immediately. You can GET and SET one after the other, but you won't get the 'correct' info in the SET command until one sim cycle later.
 
I know what you are talking about but I don't think it is the explanation here. If it was, I would have a difference of 0.55s between the 2 values and I have much more... The strange thing is that my time value printed in the console has decimals (of course, it is changed at 18 Hz so obviously it is not an integer when I get it in seconds) but the value I set has no decimal, it is always XXX.000.
I will make some in-game screenshots to let you see how it looks.
I'm afraid I will have to find a workaround because I don't think this problem has a solution.
 
Here are some screenshots:
The first one shows the console, in which I print the current time that I get from ABSOLUTE TIME, and as soon as the aircraft leaves the ground, I store the time value in another variable, then I show the time and the flying time (time elapsed since the aircraft left the ground).

1639435476024.png

The logs that you see here uses my JavaScript variables only, and you see it works perfect.
In this example, the aircraft took off exactly at time 63775032020.68266.
As it is said in the log, I store this value in a local var named MY_TAKEOFF_TIME. And when I look at the in-game panel that shows the local vars, here is what I see:

1639435633174.png


Can you see the difference?
The value stored in the local var is NOT the value I stored in it. It is close, but it is not the same and I can't explain this difference.
 
I've always had problems storing absolute time in L: vars. I don't know what's going on but the sim just doesn't seem to be able to store numbers that large in L: vars so I've always used Local Time or Time of Day.

You could try splitting the number and storing the millions in one L:var and the remainder in another. If you were really keen you could try to find out the exact point where L:vars fail to store large numbers.
 
Thanks for your feedback, I am happy to see I am not the only one having problems to store large numbers in L vars. That is surprising because when I used to code in C++, any SIM Var or local Var was stored in a 64 bits float, which was right for this kind of value.
Anyway, as I said, I will have to find a workaround and your idea is good, I thank you for this. I have another one: get the time at which the sim starts and store it in a variable, like simStartTime. Then instead of using ABSOLUTE TIME, I would use the sim time, that is ABSOLUTE TIME - simStartTime. Doing this I will manage small numbers and I hope it would be OK.
 
That's a very good spot - I use E:ABSOLUTE TIME a lot but I've never tried storing it in an L:VAR. A good illustration it's likely to be a word truncation issue is that

Code:
63775032020 = 111011011001010010011100001011010100
63775031296 = 111011011001010010011100000000000000

(note the right hand zeroes).
 
You're probably right, indeed it looks like a truncation. I cannot explain why they did this but it is certainly the reason.
 
Back
Top