Jump to content
The simFlight Network Forums

Recommended Posts

Posted

Hi John,

Below are two tiny Lua scripts to calculate the total weight (e.g., landing weight) of a plane (Learjet in this case). If I run the scripts a number of times as the plane flies and burns off fuel, the first two-line script using an offset works reliably and always provides the correct updated weight.

However, the second script that creates the Lvar Lear_LDG_Weight in order to read the TOTAL WEIGHT Avar fails approximately 40 to 50 percent of the time. When it fails the script usually reports the initial value used when creating the Lvar (the 10361 value), or just doesn't update from the previous value.  Using the sim's SimVar tool I monitored the TOTAL WEIGHT simvar and it always was continually updating as expected. I assume the (0x30C0) offset is using that simvar value. 

The main purpose of this post is to make sure I understand the Lvar creation process. It was the "Lvar script" that I tried first until seeing it was not working reliably. If the code in the Lvar script looks correct to you, then I would assume the sim may have some kind of a timing issue. This happens in both MSFS2020 and MSFS2024. 

I know you are on Holiday and this issue can certainly wait until a convenient time.

Thanks,

Al

ldg_wt = ipc.readDBL(0x30C0)                       -- get total weight
ldg_wt = math.floor(ldg_wt_offset + 0.5)       -- round to no decimal places

====================================================================================================

 ipc.createLvar("L:Lear_LDG_Weight",10361)                                                  -- create Lvar, just using Lear's empty weight as the initial value
 ipc.reloadWASM()
 ipc.sleep(1000)
 ipc.execCalcCode(" (A:TOTAL WEIGHT, pounds) (>L:Lear_LDG_Weight) ")    -- get current total weight
 ipc.sleep(100)
 ldg_wt = ipc.readLvar("L:Lear_LDG_Weight")
 ldg_wt = math.floor(ldg_wt + 0.5) 

  • ark1320 changed the title to Creating Lvar Timing Issue?
Posted (edited)
On 8/16/2025 at 3:45 AM, ark1320 said:

The main purpose of this post is to make sure I understand the Lvar creation process. It was the "Lvar script" that I tried first until seeing it was not working reliably. If the code in the Lvar script looks correct to you, then I would assume the sim may have some kind of a timing issue. This happens in both MSFS2020 and MSFS2024. 

There are a few things that could be improved in the script:

1. You only need to create the lvar once, do you should handle this if the script is being called multiple times.
2. If you create an lvar using ipc.creatLvar, you don't need to reload the WASM afterwards - this is done automatically.

As for the timing issue, lvar values are, by default, updated and send out from the WASM at a frequency of 6Hz, so roughly every 166ms.Therefore a delay of 100ms is not going to be enough most of the time, although this depends on the timings. Taking into account the time needed to send and process the request, you would need a delay of 200ms to make sure the new value had been received. if you want faster updates, you can set the WASM ini parameter LvarUpdateFrequency to VisualFrame.

 

if ipc.getLvarId("L:Lear_LDG_Weight") == nil then
  ipc.createLvar("L:Lear_LDG_Weight",10361) 
  ipc.sleep(600)
end
ipc.execCalcCode(" (A:TOTAL WEIGHT, pounds) (>L:Lear_LDG_Weight) ")    -- get current total weight

ipc.sleep(200)
ldg_wt = ipc.readLvar("L:Lear_LDG_Weight")
ldg_wt = math.floor(ldg_wt + 0.5) 

 

Edited by John Dowson
typo corrected
Posted

Even better:

if ipc.getLvarId("L:Lear_LDG_Weight") == nil then
  ipc.createLvar("L:Lear_LDG_Weight",10361)
  while (ipc.getLvarId("L:Lear_LDG_Weight") == nil) do
    ipc.sleep(50)
  end
end
ipc.execCalcCode(" (A:TOTAL WEIGHT, pounds) (>L:Lear_LDG_Weight) ")    -- get current total weight
ipc.sleep(200)
ldg_wt = ipc.readLvar("L:Lear_LDG_Weight")
ldg_wt = math.floor(ldg_wt + 0.5) 

 

Posted
34 minutes ago, John Dowson said:

Even better:

I really like your second script above and it works great. Thanks very much! A good lesson for me.

BTW, your first script above has a typo in the first line -- the If should be Id in ipc.getLvarIf( ). 

Al

Posted
1 hour ago, ark1320 said:

BTW, your first script above has a typo in the first line -- the If should be Id in ipc.getLvarIf( ). 

Yes - I saw that when I made the second script and corrected it!

1 hour ago, ark1320 said:

I really like your second script above and it works great. Thanks very much! A good lesson for me.

As reading the value of an lvar by name is quite time consuming (as it has to go through the list of available lvars) , the script cant also be more efficient:

id = ipc.getLvarId("L:Lear_LDG_Weight")
if  id == nil then
  ipc.createLvar("L:Lear_LDG_Weight",10361)
  while (id == nil) do
    ipc.sleep(50)
    id = ipc.getLvarId("L:Lear_LDG_Weight")
  end
end
ipc.execCalcCode(" (A:TOTAL WEIGHT, pounds) (>L:Lear_LDG_Weight) ")    -- get current total weight
ipc.sleep(200)
ldg_wt = ipc.readLvarById(id)
ldg_wt = math.floor(ldg_wt + 0.5) 

Basically if you are accessing an lvar by name more than once, it is better to get the id of the lvar and use the id functions.

John

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.