Question [SOLVED] Importing Lua Module?

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
More Lua questions :(

I am trying to clean up my vessel script by moving the animations to a separate module file. I am basically following the format shown here: https://copyprogramming.com/howto/in-lua-how-do-you-import-modules

This is my module file make_anim.lua (in same directory as the vessel script):

Code:
local make_anim = {}

function make_anim.Elevator()

    anim_elevator = vi:create_animation(0.5)

    elevator =
        {
        type =  'rotation',
        mesh =  0,
        grp =   {134, 142},
        ref =   {x=0,y=-0.9223039,z=-4.268489},
        axis =  {x=1,y=0,z=0},
        angle = 33*RAD
        }
  
    vi:add_animationcomponent(anim_elevator,0,1,oapi.create_animationcomponent(elevator))

end

return make_anim

I am attempting to load it in my vessel script (in the Config/Vessels directory, along with the aforementioned module file) with:

Code:
make_anim_module = require("make_anim")

and calling the function with:

Code:
make_anim.Elevator()

The vessel script runs, but none of these animations work. If I copy all the function back into my vessel script everything works fine, but I can't seem to get this module implemented. I'm at my wit's end with Lua. I thought it was going to be easier than coding C++ modules but it seems it is a whole new circus of syntax that I can't grok.
 

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
2,064
Reaction score
507
Points
113
MAybe that was just a typo in your post, but shouldn't you call Elevator() on make_anim_module, when you've used require(...) ?
Like:
Code:
make_anim_module = require("make_anim")
...
make_anim_module.Elevator()
On the other hand, I am only slightly above noob-level when it comes to Lua ;)
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
MAybe that was just a typo in your post, but shouldn't you call Elevator() on make_anim_module, when you've used require(...) ?
Like:
Code:
make_anim_module = require("make_anim")
...
make_anim_module.Elevator()
On the other hand, I am only slightly above noob-level when it comes to Lua
That may be, but I tried it and I get no change. As a matter of fact, quite a lot of the code following the attempt to run the require doesn't execute, so there is something wrong with the syntax and it just halts there. I don't know if there is an IDE that I can use with Lua to help with syntax errors.
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,877
Reaction score
2,131
Points
203
Location
between the planets
That may be, but I tried it and I get no change. As a matter of fact, quite a lot of the code following the attempt to run the require doesn't execute, so there is something wrong with the syntax and it just halts there. I don't know if there is an IDE that I can use with Lua to help with syntax errors.
There's a lua extension for visual studio code (not to be confused with visual studio, completely different animal), because there's an extension for just about anything for visual studio code:

VS code is a really great editor to use even without extensions, by the way.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
There's a lua extension for visual studio code (not to be confused with visual studio, completely different animal), because there's an extension for just about anything for visual studio code:

VS code is a really great editor to use even without extensions, by the way.
I'm familiar with VS Code, but didn't know about this extension. That sounds like what I need. Thanks! I'll work on this this evening.
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,877
Reaction score
2,131
Points
203
Location
between the planets
I'm familiar with VS Code, but didn't know about this extension.
The general rule of thumb is: If it exists, VScode has an extension for it! :p Maybe not an actually useful one, quality of extensions does vary. Takes a bit of trial and error sometimes.
(this sounds like rule 34, I'm just realising. There's a joke in there somewhere if only I can get the right pieces in the right order...)
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
Well, I'm working in VS Code with the add-on, but I am not seeing any syntax errors. It simply won't load the module file for some reason. Google gives me nothing useful. As far as I can tell it should work, but refuses.

Gnar....
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
I am wondering - how old is the Lua interperter in Orbiter? In other words, what is the latest version of Lua that Orbiter will understand? apparently Lua went through a syntax shift around version 5 and I wonder if my syntax is too modern?
 

DaveS

Addon Developer
Addon Developer
Donator
Beta Tester
Joined
Feb 4, 2008
Messages
9,434
Reaction score
689
Points
203
I am wondering - how old is the Lua interperter in Orbiter? In other words, what is the latest version of Lua that Orbiter will understand? apparently Lua went through a syntax shift around version 5 and I wonder if my syntax is too modern?
Not sure of the version but it has never been updated, ever. So trying looking at whatever version that was the latest around mid-2016.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
OK, I got it working. Apparently I need to add the module path to the package path. For some reason it can't see a module file in the same directory by default. This is going to make making this into a add-on annoying as I'll have to query the OS to provide the path to the local directory. And I'm on Linux, so I'm sure it's something else for Windows.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
In particular, this is how to add the path:

local module_folder = "path_to_directory_containing_this_script"
package.path = module_folder .. "?.lua;" .. package.path
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,877
Reaction score
2,131
Points
203
Location
between the planets
OK, I got it working. Apparently I need to add the module path to the package path.
That is not surprising, as the modules folder would not be the working directory when it is loaded by orbiter. The working directory will most likely be the location of the orbiter executable. I think you should be able to handle this with a relative path, which shouldn't be OS specific. Also, the orbiter API provides local folder locations, though I am not sure if the LUA api exposes these functions.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
That is not surprising, as the modules folder would not be the working directory when it is loaded by orbiter. The working directory will most likely be the location of the orbiter executable. I think you should be able to handle this with a relative path, which shouldn't be OS specific. Also, the orbiter API provides local folder locations, though I am not sure if the LUA api exposes these functions.
Apparently Lua doesn't handle relative paths very easily. You have to add the path to the package.path. I did not think about using the OrbiterAPI to get that path, good thought. I'll look into that.

Anyway, the modules work nicely. It's actually fairly easy to modularize the code as compared to C++. I even got keypress animation triggers working for the landing gear and canopy following this: https://www.orbiter-forum.com/threads/trying-to-implement-keypress-detection-in-lua-solved.41000/ Rather nice to be able to develop add-ons without having to compile C++ modules.
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,877
Reaction score
2,131
Points
203
Location
between the planets
It's actually fairly easy to modularize the code as compared to C++
I do remember LUA not being too bad to work with, though it's a long time ago.
However, if a project gets more complex, you will really start to miss compile time type checking, as you do with any dynamically typed language.
 
Top