Programming Question Lua how to send a command key to a vessel

jacquesmomo

Addon Developer
Addon Developer
Joined
Jun 14, 2008
Messages
810
Reaction score
860
Points
108
Location
FRANCE
Website
francophone.dansteph.com
Hi

I need one more time your help with Lua...

I would simply to send a command (press the P key) to my "Ariane6" vessel (for initiating the multistage autopilot when the SCN starts).

This is similar to what can be done with Orbitersound with the following command:
SENDVESSELKEY T:+0.1 Key:OAPI_KEY_P Vessel:Ariane6
(COMMAND - simulation time - Key - vesel name)

I looked in the documentation and couldn't find anything...

Thanks in advance for your help.
 
Hi

I need one more time your help with Lua...

I would simply to send a command (press the P key) to my "Ariane6" vessel (for initiating the multistage autopilot when the SCN starts).

This is similar to what can be done with Orbitersound with the following command:
SENDVESSELKEY T:+0.1 Key:OAPI_KEY_P Vessel:Ariane6
(COMMAND - simulation time - Key - vesel name)

I looked in the documentation and couldn't find anything...

Thanks in advance for your help.
OrbiterSound only works with C++ in Orbiter 2016 IIRC. If you want to use Lua on Orbiter2024 you need to use XRSound. Documentation for using xrsound is available in the Orbiter Lua Script interface documentation.
 
OrbiterSound only works with C++ in Orbiter 2016 IIRC. If you want to use Lua on Orbiter2024 you need to use XRSound. Documentation for using xrsound is available in the Orbiter Lua Script interface documentation.
Yes, I know...
But I'm just looking for how to "send" a command key to a ship in a .lua file. (P or G for instance)

something like that :
oapi.simulatebufferedkey(OAPI_KEY.G)

but this doesn't works...
 
Does the oapi.simulatebufferedkey work at all?
I mean, could you test some key commands that are not meant fo a vessel, but for Orbiter itself - like "time-acceleration" for example ("T" or "Z")?
...just to see if that works at all.
Another try might be to use the "unbuffered" version (simulateimmediatekey if I remember correctly), does that behave different?
 
Does the oapi.simulatebufferedkey work at all?
I mean, could you test some key commands that are not meant fo a vessel, but for Orbiter itself - like "time-acceleration" for example ("T" or "Z")?
...just to see if that works at all.
Another try might be to use the "unbuffered" version (simulateimmediatekey if I remember correctly), does that behave different?
Hello Kuddel :cheers:

And thank you for your reply. :hailprobe:


So I tried this:

proc.wait_simdt(10)
oapi.simulatebufferedkey(OAPI_KEY.T)
proc.wait_simdt(20)
oapi.simulatebufferedkey(OAPI_KEY.R)


And it works !!

Then I tried this:

proc.wait_simdt(10)
oapi.simulateimmediatekey(OAPI_KEY.T)
oapi.simulateimmediatekey(OAPI_KEY.P)

and it doesn't work (neither T nor P)

Then I tried this :

proc.wait_simdt(10)
oapi.simulatebufferedkey(OAPI_KEY.T)
proc.wait_simdt(20)
oapi.simulatebufferedkey(OAPI_KEY.R)
proc.wait_simdt(5)
oapi.simulatebufferedkey(OAPI_KEY.P)


And unfortunately, the "P" command doesn't work. (T and R yes)

I think the oapi.simulatebufferedkey function sends the command to Orbiter, but not to the vessel.
  • P is to initiates the Multistage autopilot (guidance.txt file)
  • but P has no function in the Orbiter kernel.

Maybe I need to tell the function to send the "P" command to the vessel ?

So, at the moment, I still don't know how to do this... 😢
If you have any ideas...


[ EDIT ]

But.... I'm stupid... 🙄

I forgot that there's a command related to multistage to write in the scn file:
GNCRUN 1
quite simply...

This command allows to launch the "multistage" autopilot when launching the scenario
That solves my problem...

Thanks again for your help :salute:, I'll definitely have other questions about "Lua" as I am a beginner with Lua....
 
Last edited:
Have you tried vessel:send_bufferedkey (keycode)?
Yes... but it does't works...

With
Vessel:send_bufferedkey (OAPI_KEY.P)
I have this :

./Script/Ariane6/automatique 10s.lua:1: attempt to call method 'send_bufferedkey' (a nil value)
stack traceback:
./Script/Ariane6/automatique 10s.lua:1: in main chunk
[C]: in function 'dofile'
./Script/oapi_init.lua:28: in function 'run'
[string "line"]:1: in main chunk


and if I write instead of "vessel" my vessel name (Ariane6) :
Ariane6:send_bufferedkey (OAPI_KEY.P)
I have this :

./Script/Ariane6/automatique 10s.lua:1: attempt to index global 'Ariane6' (a nil value)
stack traceback:
./Script/Ariane6/automatique 10s.lua:1: in main chunk
[C]: in function 'dofile'
./Script/oapi_init.lua:28: in function 'run'
[string "line"]:1: in main chunk

:unsure:
 
Looking at some source-code for hints (Script/Atlantis/launch.lua in this case) I found this:
You could try something like this (not testet, just written "blind"):
C++:
vif = vessel.get_focusinterface()
if vif:get_classname() ~= 'Ariane6' then  -- Class-Name is a guess from me ;)
  term.out('**** WARNING: Unexpected vessel type')
  term.out('**** '..vif:get_classname())
end
vif:send_bufferedkey(ktable.P) -- ktable is defined in oapi_init.lua (https://github.com/orbitersim/orbiter/blob/328b2e4752a87f658670ea9eacae4442ed3a221d/Script/oapi_init.lua#L18)
 
By the way:
The API Documentation can be found here: Html/Main/Script/mtd_vessel.htm [link]
On github "viewing" it is a bit difficult, so I downloaded the complete Script folder locally to "browse it" ;)
 
You could try something like this (not testet, just written "blind"):
YES !!! :salute: It works!!!
Thanks, you gave me my third lesson. :hailprobe:

(The first was proc.wait_simdt(<time>)
and the second was how to display text on the screen.)

Here's what I did (just for testing)

ktable = {Q=0x10, W=0x11, E=0x12, R=0x13, T=0x14, Y=0x15, U=0x16, I=0x17,
O=0x18, P=0x19, A=0x1E, S=0x1F, D=0x20, F=0x21, G=0x22, H=0x23,
J=0x24, K=0x25, L=0x26, Z=0x2C, X=0x2D, C=0x2E, V=0x2F, B=0x30,
N=0x31, M=0x32}


vif = vessel.get_focusinterface()
if vif:get_classname() ~= 'Ariane6' then
term.out('**** WARNING: Unexpected vessel type')
term.out('**** '..vif:get_classname())
end
vif:send_bufferedkey(ktable.P)


vif = vessel.get_focusinterface()
if vif:get_classname() ~= 'Ariane6' then
term.out('**** WARNING: Unexpected vessel type')
term.out('**** '..vif:get_classname())
end
vif:send_bufferedkey(ktable.J)


vif = vessel.get_focusinterface()
if vif:get_classname() ~= 'Ariane6' then
term.out('**** WARNING: Unexpected vessel type')
term.out('**** '..vif:get_classname())
end
vif:send_bufferedkey(ktable.F)

Just a quick question: ;)

What are these two lines for?
term.out('**** WARNING: Unexpected vessel type')
term.out('**** '..vif:get_classname())

As you can see, I still have a lot to learn, but you've helped me make progress.

I'll probably ask another question in a few days.

Thanks again. :cheers:
 
Those two lines were just to check if the vessel-class is acually the one we expect - not every vessel does react to "P" the same way.
So these line are more of a debug output to indicate when something is not as expected.
In "production code" that kind of error has to be handles differently of course ;)
term.out is just printing tex to the terminal, nothing more.
 
Hi

I still need your help with LUA... 🙄
(For now, I've figured out how to display text, but that's it!!!)
Still a lot to learn...

Here's my problem:

I would like to play a sound (with a lua file).
In the Lua documentation, I found this:

xrsound:load_wav (soundID, filename, playbackType) -- Load wav file.
xrsound:play_wav (soundID[, loop=false[, volume=1.0]]) -- Play wav file.


and so I tried this:

xrsound.load_wav(1, "XRSound/Default/On.wav", PlaybackType.Global)
xrsound.play_wav(1, false, 1.0)


And it doesn't work...
And what's more, I still don't understand the error message (in red) that appears when I start my Orbiter session, so this message doesn't help me.


I'm still adapting my Ariane 6 rocket for Orbiter 2024.
It's progressing well. Despite the fact that multistage isn't working well, I've managed to work around the bugs in this module.
(But I know Matias is working on it.)
:hailprobe:
 
Hi jacquesmomo, you need to create an xrsound instance and then call its methods (something like this) :
Code:
local snd = xrsound.create_instance("xxx")
snd:load_wav(1, "XRSound/Default/Autopilot On.wav",  PlaybackType.InternalOnly)
snd:play_wav(1)
 
Hi Gondos

Great!
It works!

Just one question:

For xxx in local snd = xrsound.create_instance("xxx")
Can we enter any value, or are there numbers that should be avoided?

(I set "111")

Thanks again
 
Usually you give it your vessel interface but if your module is not a vessel (e.g. an MFD) then you can pass an arbitrary string.
 
Back
Top