How to set key press response in script

xjflcf520

Member
Joined
Feb 26, 2021
Messages
61
Reaction score
5
Points
23
Location
Chana
I want to set in the script file of orbiter2016 to automatically press a button at a certain moment so that the spacecraft can make corresponding animations.
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
7,640
Reaction score
796
Points
188
Location
Toulouse
LUA works well for things like ascent scripts. That's the way Martins originally used it, as an ascent program computer for Space Shuttle Atlantis.
 

xjflcf520

Member
Joined
Feb 26, 2021
Messages
61
Reaction score
5
Points
23
Location
Chana
LUA works well for things like ascent scripts. That's the way Martins originally used it, as an ascent program computer for Space Shuttle Atlantis.
So what do I need to do to make orbiter automatically respond to buttons and automatically generate animation
 

n72.75

Addon Developer
Addon Developer
Tutorial Publisher
Donator
Joined
Mar 21, 2008
Messages
1,923
Reaction score
310
Points
83
Location
Biddeford ME
Website
www.adabsurdumpublishing.com
What are you trying to do? If you're making your own addon and you want it to have animations that start when a certain key is pressed, e.g. gear up/down then you can clbkConsumeBufferedKey() in your C++ vessel code, there's also SendBufferedKey() (another C++ function, which you could use on any VESSEL2 pointers you have).

But this requires writing your own orbiter module, if you have C++ experience we can probably talk you through something, but if you've never programmed in C++ you should probably start with the basics.

The pages for these functions are 632, and 682 of the Orbiter 2016 API reference guide.
 

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,214
Reaction score
474
Points
123
Location
Lisbon
Website
orbiterspaceport.blogspot.com
LUA works well for things like ascent scripts. That's the way Martins originally used it, as an ascent program computer for Space Shuttle Atlantis.
Based on the docs Lua was supposed to be able to do everything, including keypresses, complete ship parameter definition, etc.
The problem is that the interpreter is incomplete. At least keyboard and landing point functions aren't implemented.

Regarding the OP's question besides C++ you can use VesselBuilder.
For a simple "press key - animation + change parameter" situation VesselBuilder is enough.
 

xjflcf520

Member
Joined
Feb 26, 2021
Messages
61
Reaction score
5
Points
23
Location
Chana
Based on the docs Lua was supposed to be able to do everything, including keypresses, complete ship parameter definition, etc.
The problem is that the interpreter is incomplete. At least keyboard and landing point functions aren't implemented.

Regarding the OP's question besides C++ you can use VesselBuilder.
For a simple "press key - animation + change parameter" situation VesselBuilder is enough.
Yes, I want to know how to realize the function of automatic retraction and retraction of landing gear in the module of making spaceship with C + +
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
7,640
Reaction score
796
Points
188
Location
Toulouse
C++ is different from LUA, it is the native language in which Orbiter is coded. But there's no specific function like 'retract landing gear'. You'll have to define one, or probably a sequence of animations. Then you can have it triggered by a key or key combo, and implement it in a physical way (the ground contact points of the vessel should change).

Look for APIGuide in the SDK/doc folder, Martins takes a landing gear animation as an example. You also have a chm 'help' file that reference all the functions and classes.

If you never used C++ before, don't be shy and go install MS Visual Studio (2017 or 2019), I'm the living proof you don't need a computer science background for that. It's a fine hobby. There are tutorials specific to Orbiter all over the Forums, even on YouTube, and I and others will be happy to help you.
 

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
1,665
Reaction score
191
Points
63
By the way, have you looked at >> this thread << ?
I think that for general vessel control there is quite a lot of features you can use.
If you really need to simulate a specific button-press (let's say flip one specific switch in NASSP), you are out of luck;
but getting to move a vessel around (thrust, attitude, etc.) the current set of lua functions should get you quite far.

For gear retraction/extension there is (currently?) no "easy" set_gear_state(up), set_gear_state(down) & get_gear_state() methods available.
I have to check whether these might be needed, or if the get_status() rsp. set_status() are enough to work with.
I can't remember out of my head what the 'flags' in those structures represent...
 

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
1,665
Reaction score
191
Points
63
Oh, now that I've thought about it: There will be no set_gear_state(...) or get_gear_state() methods as not every vessel has a landing-gear!
An exception is any vessel (module) that provides those extra functionality (like the DeltaGlider for example, where a V:Gear(UP) would work)!
 
Last edited:

dgatsoulis

ele2png user
Joined
Dec 2, 2009
Messages
1,779
Reaction score
27
Points
63
Location
Sparta
Last time I messed with LUA (years ago) it wasn't possible. I gave up on it because of that; implementation is incomplete.

I am pretty sure that the v:send_bufferedkey(keycode) method has existed ever since lua scripts were introduced in Orbiter back in the 2010 version. This does exactly what the OP is asking. Perhaps you are confusing it with a "wait for keypress" method, which is true that it doesn't currently exist.

To answer the original question:
I want to set in the script file of orbiter2016 to automatically press a button at a certain moment so that the spacecraft can make corresponding animations.
You can do this by using the v:send_bufferedkey(keycode) method which is described in the section Orbiter Scripting→Reference→Class methods→Vessel methods in the Orbiter.chm file, which is located in the html folder of your Orbiter installation.

Outside of lua, you can send keypresses to a vessel through a scenario command, if you have DanSteph's OrbiterSound 5.0 installed. Look for more information in the ScenarioCommandsHelp.txt file, located in the Doc\OrbiterSound Documentation\bin folder of your Orbiter installation with OrbiterSound 5.0

To my knowledge, apart from Vesselbuilder (already mentioned in this thread), these are the two methods you can use to send a keypress to a spacecraft in Orbiter, that rely on editing simple text files (.lua and .scn).

Here is an example of sending a keypress through an OrbiterSound 5.0 scenario command:

(The following goes into your scenario file, before the BEGIN_SHIPS ... END_SHIPS section).
Code:
BEGIN_OrbiterSound
SENDVESSELKEY T:10 Key:OAPI_KEY_G                Vessel:GL-01
END

This will press the "G" key for the vessel with the name "GL-01" 10 seconds after the scenario starts.
Scenario commands are limited in the way they can be triggered, as the only input they use is time, measured in either seconds passed since the start of the scenario, or seconds passed since the last command was issued.

A lua script is much more versatile in this regard as you can use a vast number of triggers from the existing lua functions. Here is an example of a lua script that will perform the same task as the previous example (press the "G" key for the vessel with the name "GL-01" 10 seconds after the scenario starts. )

Code:
v = vessel.get_interface('GL-01')

goals = 0
while goals < 1 do
   simt = oapi.get_simtime()
   proc.skip()
   if simt > 10 then goals = 1 end
   if goals > 0 then  end
end  

v:send_bufferedkey(OAPI_KEY.G)

Any number of triggers can be used to achieve the same goal. This next example presses the G key for the vessel named "GL-01" when it reaches an altitude of 10 meters above ground.

Code:
v = vessel.get_interface('GL-01')

goals = 0
while goals < 1 do
   alt = v:get_altitude(ALTMODE.GROUND)
   proc.skip()
   if alt > 10 then goals = 1 end
   if goals > 0 then  end
end  

v:send_bufferedkey(OAPI_KEY.G)

Hope this helps,
:cheers:
 
Last edited:

xjflcf520

Member
Joined
Feb 26, 2021
Messages
61
Reaction score
5
Points
23
Location
Chana
I am pretty sure that the v:send_bufferedkey(keycode) method has existed ever since lua scripts were introduced in Orbiter back in the 2010 version. This does exactly what the OP is asking. Perhaps you are confusing it with a "wait for keypress" method, which is true that it doesn't currently exist.

To answer the original question:

You can do this by using the v:send_bufferedkey(keycode) method which is described in the section Orbiter Scripting→Reference→Class methods→Vessel methods in the Orbiter.chm file, which is located in the html folder of your Orbiter installation.

Outside of lua, you can send keypresses to a vessel through a scenario command, if you have DanSteph's OrbiterSound 5.0 installed. Look for more information in the ScenarioCommandsHelp.txt file, located in the Doc\OrbiterSound Documentation\bin folder of your Orbiter installation with OrbiterSound 5.0

To my knowledge, apart from Vesselbuilder (already mentioned in this thread), these are the two methods you can use to send a keypress to a spacecraft in Orbiter, that rely on editing simple text files (.lua and .scn).

Here is an example of sending a keypress through an OrbiterSound 5.0 scenario command:

(The following goes into your scenario file, before the BEGIN_SHIPS ... END_SHIPS section).
Code:
BEGIN_OrbiterSound
SENDVESSELKEY T:10 Key:OAPI_KEY_G                Vessel:GL-01
END

This will press the "G" key for the vessel with the name "GL-01" 10 seconds after the scenario starts.
Scenario commands are limited in the way they can be triggered, as the only input they use is time, measured in either seconds passed since the start of the scenario, or seconds passed since the last command was issued.

A lua script is much more versatile in this regard as you can use a vast number of triggers from the existing lua functions. Here is an example of a lua script that will perform the same task as the previous example (press the "G" key for the vessel with the name "GL-01" 10 seconds after the scenario starts. )

Code:
v = vessel.get_interface('GL-01')

goals = 0
while goals < 1 do
   simt = oapi.get_simtime()
   proc.skip()
   if simt > 10 then goals = 1 end
   if goals > 0 then  end
end 

v:send_bufferedkey(OAPI_KEY.G)

Any number of triggers can be used to achieve the same goal. This next example presses the G key for the vessel named "GL-01" when it reaches an altitude of 10 meters above ground.

Code:
v = vessel.get_interface('GL-01')

goals = 0
while goals < 1 do
   alt = v:get_altitude(ALTMODE.GROUND)
   proc.skip()
   if alt > 10 then goals = 1 end
   if goals > 0 then  end
end 

v:send_bufferedkey(OAPI_KEY.G)

Hope this helps,
:cheers:
Thank you very much for your detailed methods, let me learn a lot
 

xjflcf520

Member
Joined
Feb 26, 2021
Messages
61
Reaction score
5
Points
23
Location
Chana
C++ is different from LUA, it is the native language in which Orbiter is coded. But there's no specific function like 'retract landing gear'. You'll have to define one, or probably a sequence of animations. Then you can have it triggered by a key or key combo, and implement it in a physical way (the ground contact points of the vessel should change).

Look for APIGuide in the SDK/doc folder, Martins takes a landing gear animation as an example. You also have a chm 'help' file that reference all the functions and classes.

If you never used C++ before, don't be shy and go install MS Visual Studio (2017 or 2019), I'm the living proof you don't need a computer science background for that. It's a fine hobby. There are tutorials specific to Orbiter all over the Forums, even on YouTube, and I and others will be happy to help you.
Thank you very much for your kind answer
 

xjflcf520

Member
Joined
Feb 26, 2021
Messages
61
Reaction score
5
Points
23
Location
Chana
I am pretty sure that the v:send_bufferedkey(keycode) method has existed ever since lua scripts were introduced in Orbiter back in the 2010 version. This does exactly what the OP is asking. Perhaps you are confusing it with a "wait for keypress" method, which is true that it doesn't currently exist.

To answer the original question:

You can do this by using the v:send_bufferedkey(keycode) method which is described in the section Orbiter Scripting→Reference→Class methods→Vessel methods in the Orbiter.chm file, which is located in the html folder of your Orbiter installation.

Outside of lua, you can send keypresses to a vessel through a scenario command, if you have DanSteph's OrbiterSound 5.0 installed. Look for more information in the ScenarioCommandsHelp.txt file, located in the Doc\OrbiterSound Documentation\bin folder of your Orbiter installation with OrbiterSound 5.0

To my knowledge, apart from Vesselbuilder (already mentioned in this thread), these are the two methods you can use to send a keypress to a spacecraft in Orbiter, that rely on editing simple text files (.lua and .scn).

Here is an example of sending a keypress through an OrbiterSound 5.0 scenario command:

(The following goes into your scenario file, before the BEGIN_SHIPS ... END_SHIPS section).
Code:
BEGIN_OrbiterSound
SENDVESSELKEY T:10 Key:OAPI_KEY_G                Vessel:GL-01
END

This will press the "G" key for the vessel with the name "GL-01" 10 seconds after the scenario starts.
Scenario commands are limited in the way they can be triggered, as the only input they use is time, measured in either seconds passed since the start of the scenario, or seconds passed since the last command was issued.

A lua script is much more versatile in this regard as you can use a vast number of triggers from the existing lua functions. Here is an example of a lua script that will perform the same task as the previous example (press the "G" key for the vessel with the name "GL-01" 10 seconds after the scenario starts. )

Code:
v = vessel.get_interface('GL-01')

goals = 0
while goals < 1 do
   simt = oapi.get_simtime()
   proc.skip()
   if simt > 10 then goals = 1 end
   if goals > 0 then  end
end 

v:send_bufferedkey(OAPI_KEY.G)

Any number of triggers can be used to achieve the same goal. This next example presses the G key for the vessel named "GL-01" when it reaches an altitude of 10 meters above ground.

Code:
v = vessel.get_interface('GL-01')

goals = 0
while goals < 1 do
   alt = v:get_altitude(ALTMODE.GROUND)
   proc.skip()
   if alt > 10 then goals = 1 end
   if goals > 0 then  end
end 

v:send_bufferedkey(OAPI_KEY.G)

Hope this helps,
:cheers:
Hello, I want to ask if I can get the longitude and latitude of the spaceship in Lua and display it directly through anotation
 

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
1,665
Reaction score
191
Points
63
Something like this would do the job:

SQL:
-- ---------------------------------------------------------------------------
-- Some useful general constants
-- ---------------------------------------------------------------------------
PI=3.14159265358979    -- pi
PI05=1.57079632679490  -- pi/2
RAD=PI/180.0           -- deg->rad conversion factor
DEG=180.0/PI           -- rad->deg conversion factor

-- ---------------------------------------------------------------------------
-- ANNOTATION SETUP
-- ---------------------------------------------------------------------------
-- re-use 1st 'old' annotation object (if available)
note = oapi.get_annotations() or oapi.create_annotation()
note:set_pos (0.2,0.1,0.8,0.9);
note:set_size(0.5)
note:set_colour ({r=0.7,g=0.8,b=1})

-- ---------------------------------------------------------------------------
-- Monitor background task
-- (does not terminate. Run as background job and kill when done)
-- ---------------------------------------------------------------------------
function monitor_range()
    while true do
    v = vessel.get_focusinterface()
        if v ~= nil then
            hobj = v:get_surfaceref()
            equ = oapi.global_to_equ (hobj, v:get_globalpos())

            name = v:get_name()
            lng = equ.lng *DEG
            lat = equ.lat *DEG

            note:set_text( string.format("lon: %.3f lat: %.3f [%s]", lng, lat, name))
        else
            note:set_text( "lon: n/a lat: n/a [n/a]" )
        end
        proc.wait_sysdt(0.1) -- 100ms refresh rate
    end
end

-- ---------------------------------------------------------------------------
-- MAIN
-- ---------------------------------------------------------------------------

-- Launch monitor process
proc_id = proc.bg(monitor_range)


-- proc.kill(proc_id)

I think you know how to start Lua scripts in Orbiter, do you?
In case you like to quickly see what the script is doing, I've attached a "full package" for easy "runTheScenarioAndTakeALook"[TM] ;)
 

Attachments

  • Monitor.zip
    2.3 KB · Views: 0
Last edited:

xjflcf520

Member
Joined
Feb 26, 2021
Messages
61
Reaction score
5
Points
23
Location
Chana
Something like this would do the job:

SQL:
-- ---------------------------------------------------------------------------
-- Some useful general constants
-- ---------------------------------------------------------------------------
PI=3.14159265358979    -- pi
PI05=1.57079632679490  -- pi/2
RAD=PI/180.0           -- deg->rad conversion factor
DEG=180.0/PI           -- rad->deg conversion factor

-- ---------------------------------------------------------------------------
-- ANNOTATION SETUP
-- ---------------------------------------------------------------------------
-- re-use 1st 'old' annotation object (if available)
note = oapi.get_annotations() or oapi.create_annotation()
note:set_pos (0.2,0.1,0.8,0.9);
note:set_size(0.5)
note:set_colour ({r=0.7,g=0.8,b=1})

-- ---------------------------------------------------------------------------
-- Monitor background task
-- (does not terminate. Run as background job and kill when done)
-- ---------------------------------------------------------------------------
function monitor_range()
    while true do
    v = vessel.get_focusinterface()
        if v ~= nil then
            hobj = v:get_surfaceref()
            equ = oapi.global_to_equ (hobj, v:get_globalpos())

            name = v:get_name()
            lng = equ.lng *DEG
            lat = equ.lat *DEG

            note:set_text( string.format("lon: %.3f lat: %.3f [%s]", lng, lat, name))
        else
            note:set_text( "lon: n/a lat: n/a [n/a]" )
        end
        proc.wait_sysdt(0.1) -- 100ms refresh rate
    end
end

-- ---------------------------------------------------------------------------
-- MAIN
-- ---------------------------------------------------------------------------

-- Launch monitor process
proc_id = proc.bg(monitor_range)


-- proc.kill(proc_id)

I think you know how to start Lua scripts in Orbiter, do you?
In case you like to quickly see what the script is doing, I've attached a "full package" for easy "runTheScenarioAndTakeALook"[TM] ;)
Thank you
 
Top