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

Re: Lua Script Issues

#11 Post by RoundEngine »

Well, I found one workaround which to me shouldn't be needed but I guess Lua isn't that smart.

I simply renamed the file containing the eprtable to 1somefilename so I guess it was loaded first by Lua since it comes before the other files in Windows list and when it loaded it parsed it for values.

I would have thought that nothing would be parsed until I called a function located in that file.

It's a hacked workaround that shouldn't be needed so maybe I'm missing something else.

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

Re: Lua Script Issues

#12 Post by Keith Baxter »

Hi

No, as @Corjan said library's load arbitrary and no hacking necessary. Simple lua logic

The loading of library order should be of no concern for 99.9999% of instruments.

The way you call the functions within the libraries is what needs managing. That could be within the logic.lua or within other functions.

Post a library so that we get an idea of what you are doing.

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

#13 Post by RoundEngine »

I hate computers. I was writing what I did and what files I was including and decided to go back and rerun everything with the files back at their proper names (without the number prefix) so the epr data is in eprtable_500. I uncommented the eprtable_init() call in logic.lua and it worked. That table values were all returned.

To make sure it wasn't working because get_tables was now in a file called utilities.lua I moved the function back to aircraft_tables.lua and its till worked.

I have no idea why uncommenting the call to eprtable_init() made it work with the names as they were. The eprtable_init() function was called before anything else before but didn't work then. What is even funnier is that I had removed the eprtable_init() function earlier and it does not exist anymore. Arrghh. I'll create a dummy epr_init() and call it.

It's working now so I'll continue on with it. I'm going to move some of my functions and tables around to clean things up so that should test it out.

Thanks for all the help and if it breaks again I'll be back.

Tetrachromat
Posts: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: Lua Script Issues

#14 Post by Tetrachromat »

It is worth understanding the LUA concept of "chunks" when working with code separated out into different files.

A file containing some code, is called a "chunk" when loaded in LUA. Each chunk is a separate entity with it's own scope, like code placed in a block statement (DO ... END).

When the file is loaded, that chunk is placed in the environment of the executable (in AM this is either an instrument or a panel). After all chunks (from the files in 'lib') are loaded, AM loads the last chunk from 'logic.lua'.

As has been noted already the chunks from the 'lib' folder should not run any code on loading. What is run on loading? Any function call at the chunk scope.

Good Practice would be to declare your variables holding the tables 'local' and provide global functions to access the tables. This will keep the tables private to the chunk and the functions constitute the API of your library. Call the API functions from 'logic.lua'.

If you don't follow this practices you will end up in the situation you described.

Paul

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

Re: Lua Script Issues

#15 Post by RoundEngine »

Tetrachromat wrote: Fri May 27, 2022 12:08 am It is worth understanding the LUA concept of "chunks" when working with code separated out into different files.

A file containing some code, is called a "chunk" when loaded in LUA. Each chunk is a separate entity with it's own scope, like code placed in a block statement (DO ... END).

When the file is loaded, that chunk is placed in the environment of the executable (in AM this is either an instrument or a panel). After all chunks (from the files in 'lib') are loaded, AM loads the last chunk from 'logic.lua'.

As has been noted already the chunks from the 'lib' folder should not run any code on loading. What is run on loading? Any function call at the chunk scope.

Good Practice would be to declare your variables holding the tables 'local' and provide global functions to access the tables. This will keep the tables private to the chunk and the functions constitute the API of your library. Call the API functions from 'logic.lua'.

If you don't follow this practices you will end up in the situation you described.

Paul
This is very helpful.

I'm confused as to what goes where.

Would I take the file (chunk) epr_tables and have my tables in that and the tables are declared local and in that same chunk would I have my functions to retrieve the values from the the tables and these functions are global so they will not be executed when the chunk is loaded and can be called by logic.lua. In logic.lua I have local variables to hold the results returned from the global functions.

I then do this for my other tables.

I'll work with this.

Thank you.

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

Re: Lua Script Issues

#16 Post by RoundEngine »

Thank you for all who helped in this thread. I've got a much better handle on how AM uses Lua libs and how Lua works with lib files. I wasn't happy with my file layout and organization so I redid it using what I learned here and now everything works fine.

A big thanks to everyone.

Post Reply