Lua Script Issues

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
RoundEngine
Posts: 122
Joined: Thu Sep 12, 2019 8:27 pm

Lua Script Issues

#1 Post by RoundEngine »

I'm seeing something I think is strange but then that maybe due to my limited experience with Lua.

I have a panel with several files. One file, call it A, contains some tables for vspeed calcs and a function to retrieve the proper values based on the aircraft weight. Both the tables and function are global. File A also contains a table with a list of tables to be used for that aircraft so for aircraft A it will us3 v2speedtableA, vrefspeedtableA, eprtableA. The function basically checks the list of tables, finds the aircraft model, and then returns the table names (I guess the C equivalent is pointer to the table) for the given aircraft mode. The function is called from logic.lua.

Code: Select all

aircraft_tables = {
    {
        "JustFlight Tristar 500";
        v2table_JF500;
        vreftable_JF500;
        flapsettings_JF500;
        eprtable_500;
    };
}
The function gets these tables and then uses them to look up values for the speeds, etc.

I have another table named eprtable_500,which is used to calculate EPR based on flight level and aircraft weight and is a global variable. Instead of putting it in file A I put it in file B mainly to keep like things in like files.

The function in file A is called with the aircraft type and returns values/addresses for the tables for speeds and epr. These tables are then used to calculate values for the aircraft.

The function a returns the tables for vspeeds which are contained with it in file a. However, it returns nil for eprtable_500 which is in a separate file but I thought a global could be referenced anywhere even if it's in another file so eprtable_500 should be available to file A.

How does Lua parse these lib files - is it in alphabetical order and making one pass. If so then file B variables won't be available to file A. Logic.lua sees functions in files that have names starting with letters above L. Given the functions are global and available everywhere else why not the table?

I'm at a loss here.

Also, does Air manager support require() or loadfile()? when I tried to use require or require() I get that require is nil, doesn't exist.

Thanks.

User avatar
Corjan
Posts: 2936
Joined: Thu Nov 19, 2015 9:04 am

Re: Lua Script Issues

#2 Post by Corjan »

Hi,


Air Manager first loads all files in the lib folder, then it loads the logic.lua.
There is no guarentee in what order the libraries are loaded. Technically it is the way the OS presents it to AM, it can be any order the OS feels like.

Try to not to execute any code while the libraries are loaded. Instead you can make resources and function available, but make sure that the code in the logic.lua file dictated the order.

Require and loadfile are not available since you could load files outside of the instrument folder that way, and breaking compatibility between OS's and computers.


Hope this makes sense,

Corjan

RoundEngine
Posts: 122
Joined: Thu Sep 12, 2019 8:27 pm

Re: Lua Script Issues

#3 Post by RoundEngine »

Corjan wrote: Thu May 26, 2022 8:14 am Hi,


Air Manager first loads all files in the lib folder, then it loads the logic.lua.
There is no guarentee in what order the libraries are loaded. Technically it is the way the OS presents it to AM, it can be any order the OS feels like.

Try to not to execute any code while the libraries are loaded. Instead you can make resources and function available, but make sure that the code in the logic.lua file dictated the order.

Require and loadfile are not available since you could load files outside of the instrument folder that way, and breaking compatibility between OS's and computers.


Hope this makes sense,

Corjan
Yes, it makes perfect sense. I can see why require and loadfile are not available then.

How do I ensure no code is executed while the libraries load? My logic.lua has the functions for handling the subscription callbacks which do use the library programs, then it has some initialization to initialize graphics, and get the addresses of the tables for the vspeeds and performance parameters, and finally the subscriptions.

{Functions}
{Init graphics, get user properties and table addresses (tables are in files under lib directory)}
{Subscriptions}

Thank you.

User avatar
Ralph
Posts: 7878
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Lua Script Issues

#4 Post by Ralph »

I'm not a Lua expert. But what we/I usually do is, place the library stuff within a function, then call that function from the logic.lua.
You can also give an argument together with that function, like a 'visible' variable which can be true or false for example. Or you call a generic function with a certain argument, which then calls specific functions within those libraries. Lots of options.

RoundEngine
Posts: 122
Joined: Thu Sep 12, 2019 8:27 pm

Re: Lua Script Issues

#5 Post by RoundEngine »

That's what I do, too. All my libs contain functions called from logic.lua.

The issue I'm having is that I have one lib that contains a function that calculates speed values based on aircraft weight and flap setting. That lib contains the function and tables for speeds. I also need to do performance calculations such as EPR, N1 based on altitude and weight and I have tables for that that I use. To keep things organized and control the size of files I put the performance table in another lib with the table name being global. However, the function in lib A keeps saying that the table in lib B is undefined, nil so it's not seeing it.

I suspect one reason is that Windows loads the files in alphabetical order and lib A is before lib B and Lua is, as I understand it, a single pass compile but a previous post mentioned something about being able to make resources and functions available and code in logic.lua dictating that order but I'm not sure how to do that.

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Lua Script Issues

#6 Post by Keith Baxter »

Hi,

I create a library "bit_and_bobs" that contains all font styles, tables and canvas_draw() etc...

This is called first in the logic.lua before all the other init functions are called. Something along these lines. Note each init is a library function. Z order always apples in stacking your library's

Code: Select all

---General used items						
bits_and_bobs_init()
tables_libary_init()
main_background_init()
radio_init()
powerup_pages_init()

This way, as long as you table or group is declared before the library init is called you will not return errors.

You can add dummy "EMPTY" groups and other elements within this bits_and_bobs library. Then populate them with code within the library chosen.

Code: Select all

b200_eng_group=group_add()
C90_eng_group=group_add()
G36_eng_group=group_add()
G58_eng_group=group_add()





Keith
Last edited by Keith Baxter on Thu May 26, 2022 8:33 pm, edited 1 time in total.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

RoundEngine
Posts: 122
Joined: Thu Sep 12, 2019 8:27 pm

Re: Lua Script Issues

#7 Post by RoundEngine »

Keith Baxter wrote: Thu May 26, 2022 8:17 pm Hi,

I create a library "bit_and_bobs" that contains all font styles, tables and canvas_draw() etc...

This is called first in the logic.lua before all the other init functions are called. Something along these lines. Note each init is a library function. Z order always apples in stacking your library's

Code: Select all

---General used items						
bits_and_bobs_init()
tables_libary_init()
main_background_init()
radio_init()
powerup_pages_init()

Keith
Hmm. Let me see if I understand what Lua and my OS are doing and how you work around it.

If I call my libB which contains only tables I can do

libB_init()
libA_init()
init others in proper order.

I assume that lib B will be set up first and any tables it has will be loaded and available for libA.

Can I also assume that libB_init() can be an empty function that simply does nothing so logic.lua will be forced to load the libB file thereby loading any tables which become available for following libraries?

This has been very helpful. In the back of my mind I knew z order applied to initializing when I had functions to call in those libraries but this time I simply have a library that has only tables so I ran into the OS load order.

Thank you.

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Lua Script Issues

#8 Post by Keith Baxter »

RoundEngine wrote: Thu May 26, 2022 8:29 pm
Keith Baxter wrote: Thu May 26, 2022 8:17 pm Hi,

I create a library "bit_and_bobs" that contains all font styles, tables and canvas_draw() etc...

This is called first in the logic.lua before all the other init functions are called. Something along these lines. Note each init is a library function. Z order always apples in stacking your library's

Code: Select all

---General used items						
bits_and_bobs_init()
tables_libary_init()
main_background_init()
radio_init()
powerup_pages_init()

Keith
Hmm. Let me see if I understand what Lua and my OS are doing and how you work around it.

If I call my libB which contains only tables I can do

libB_init()
libA_init()
init others in proper order.

I assume that lib B will be set up first and any tables it has will be loaded and available for libA.

Can I also assume that libB_init() can be an empty function that simply does nothing so logic.lua will be forced to load the libB file thereby loading any tables which become available for following libraries?

This has been very helpful. In the back of my mind I knew z order applied to initializing when I had functions to call in those libraries but this time I simply have a library that has only tables so I ran into the OS load order.

Thank you.
Hi Yes, You got the gist.

You can always load "ghost" elements to be populated within other libraries.

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

RoundEngine
Posts: 122
Joined: Thu Sep 12, 2019 8:27 pm

Re: Lua Script Issues

#9 Post by RoundEngine »

Great. Thanks. I'm trying to convince my code to understand this <G>!

RoundEngine
Posts: 122
Joined: Thu Sep 12, 2019 8:27 pm

Re: Lua Script Issues

#10 Post by RoundEngine »

Hmm. I'm not having any luck. I changed things to. do the following in logic.lua at the end of logic.lua. This is preceded by the callback functions and the subscription items.

eprtable_init() - this is in file pertables.lua which contains the eprtable I want to use as a global. The function just prints I'm here to the console.
graphics_init() - initializes background, defines text boxes.
get_tables() - uses a table which contains the names (C equivalent - pointer to) of the speed and epr tables. It retrieves the tables for this aircraft and then returns the address for each. This also contains the tables for the V2, VRef speeds and flap settings.

get_tables() returns the correct tables for the speeds and flaps but then tells me the eprtable is null but it's been defined in eprtable_init().

I'm assuming windows loads the lib files in alphabetical order but that shouldn't matter as long as I call them in the right order in logic.lua.

Post Reply