Writing Joystick Code: Part 2: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
m (WIP template)
No edit summary
Line 100: Line 100:
   interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)
   interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)
   
   
To make it go faster, make the 30 smaller.
To make it go faster, make the 30 smaller.
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
  If you want rudder-trim, use either the repeatable or non-repeatable above, changing elevator/aileron to rudder
  If you want rudder-trim, use either the repeatable or non-repeatable above, changing elevator/aileron to rudder
Line 147: Line 147:
  U
  U
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
  start engine
  Start all engine all R
-----------------------------------------------------------------------------
  controls.startEngine(1)
  perIndexAxisHandler
  U
  controls.startEngine(0)
Start specific engine R
  controls.startEngine(1, x)
U
  controls.startEngine(0, x)
x is the engine number. First engine is 0.
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
  wingsweep
  wingsweep
Line 155: Line 163:
  weapons
  weapons
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
  ptt
  Toggle all lights on/off NR
  controls.toggleLights()
U
Switch specific lights on NR
  setProp(("controls/lighting/xxx"), true)
U
Switch specific lights off NR
  setProp(("controls/lighting/xxx"), false)
Where xxx = beacon, landing-lights, landing-light, landing-light[1] logo-lights, nav-lights, strobe, taxi-light, taxi-lights, cabin-lights, map-lights.
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
  lights; individual and all
  Instrument lights level up R
  <![CDATA[
    if(getprop("controls/lighting/instruments-norm") < 1 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.05)
    }
  ]]>
U
Instrument lights level down R
  <![CDATA[
    if(getprop("controls/lighting/instruments-norm") > 0 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.05)
    }
  ]]>
U
Instrument lights level up NR
  <![CDATA[
    if(getprop("controls/lighting/instruments-norm") < 1 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.2)
    }
  ]]>
U
Instrument lights level down NR
  <![CDATA[
    if(getprop("controls/lighting/instruments-norm") > 0 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.2)
    }
  ]]>
U
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
  step magnetos
  Step magnetos up NR
  controls.stepMagnetos(1)
U
Step magnetos down NR
  controls.stepMagnetos(-1)
U
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------



Revision as of 04:26, 13 October 2012

WIP.png Work in progress
This article or section will be worked on in the upcoming hours or days.
See history for the latest developments.

Code snippets

Make sure you have read Input device and Writing Joystick Code: Part 1 first.

These are what you use instead of Your code goes here amd Ctrl-Alt button released. You just need to put them in the right place - inbetween the <script> and </script> tags.

Please note that some of them have concepts that will be explained in a later section.

Each snippet has the following format:

Action R/NR
   Code for action
U
   Code for <mod-up>

Where Action is what you want to do, R means use with a repeatable button, NR means use with a non-repeatable button, and U is the code for <mod-up>. If it is empty, a <mod-up> is not needed.

Some Actions will have code for use with repeatable and non-repeatable buttons.

Notice that some commands have a +1 or a -1 in brackets to specify the direction of the action, and a 0 to say stop doing that.

If the action is upside-down for you, swap the + and - signs.

Also, sometimes the 1 is replaced by a number, which specifies by how much the action should be carried out. If the action is too viscious for you, make the number smaller.

The snippets


Gear down NR
  controls.gearDown(1)
U
  controls.gearDown(0)

Gear up NR
  controls.gearDown(-1)
U
  controls.gearDown(0)

Flaps down one notch NR
  controls.gearDown(1)
U
  controls.gearDown(0)

Flaps up one notch NR
  controls.gearDown(-1)
U
  controls.gearDown(0)

Deploy spoilers NR
  controls.stepSpoilers(1)
U
  controls.stepSpoilers(0)

Retract spoilers NR
  controls.stepSpoilers(-1)
U
  controls.stepSpoilers(0)

Cycle View NR
  view.stepView(1)
U

Elevator trim down R
  controls.elevatorTrim(0.75)
U

Elevator trim up R
  controls.elevatorTrim(-0.75)
U

Elevator Trim down NR
  interpolate("/controls/flight/elevator-trim", 1, 30 * (1 - getprop("controls/flight/elevator-trim")))
U
  interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)

Elevator trim up NR
  interpolate("/controls/flight/elevator-trim", -1, 30 * (1 - getprop("controls/flight/elevator-trim")))
U
  interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)

These commands are all on one line.
To make it go faster, make the 30 smaller.

Aileron trim left R
  controls.aileronTrim(-0.75)
U

Aileron trim right R
  controls.aileronTrim(0.75)
U

Aileron trim left NR
  interpolate("/controls/flight/aileron-trim", -1, 30 * (1 - getprop("controls/flight/aileron-trim")))
U
  interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)

Aileron trim right NR
  interpolate("/controls/flight/aileron-trim", 1, 30 * (1 - getprop("controls/flight/aileron-trim")))
U
  interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)

To make it go faster, make the 30 smaller.

If you want rudder-trim, use either the repeatable or non-repeatable above, changing elevator/aileron to rudder

Apply all brakes NR
  controls.applyBrakes(1)
U
  controls.applyBrakes(0)

Apply left brake NR
  controls.applyBrakes(1, -1)
U
  controls.applyBrakes(0, -1)

Apply Right brake NR
  controls.applyBrakes(1, 1)
U
  controls.applyBrakes(0, 1)

Start Stopwatch NR
  globals["__dlg:stopwatch-dialog"].start()
U  

Stop Stopwatch NR
  globals["__dlg:stopwatch-dialog"].stop()
U  

Reset Stopwatch NR
  globals["__dlg:stopwatch-dialog"].reset()
U  

Make mixture richer NR
  <![CDATA[ 
    if(getprop("controls/engines/engine/mixture") < 1 ) {
      setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)
    }
  ]]>
U

Make mixture leaner NR
  <![CDATA[
    if(getprop("controls/engines/engine/mixture") > 0 ) {
       setprop("controls/engines/engine/mixture", getprop("controls/engines/engine/mixture") - 0.05)
    }
  ]]>
U

Start all engine all R
  controls.startEngine(1)
U
  controls.startEngine(0)

Start specific engine R
  controls.startEngine(1, x)
U
  controls.startEngine(0, x)

x is the engine number. First engine is 0.

wingsweep

weapons

Toggle all lights on/off NR
  controls.toggleLights()
U

Switch specific lights on NR
  setProp(("controls/lighting/xxx"), true)
U

Switch specific lights off NR
  setProp(("controls/lighting/xxx"), false)

Where xxx = beacon, landing-lights, landing-light, landing-light[1] logo-lights, nav-lights, strobe, taxi-light, taxi-lights, cabin-lights, map-lights. 

Instrument lights level up R
  <![CDATA[ 
    if(getprop("controls/lighting/instruments-norm") < 1 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.05)
    }
  ]]>
U

Instrument lights level down R
  <![CDATA[ 
    if(getprop("controls/lighting/instruments-norm") > 0 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.05)
    }
  ]]>
U

Instrument lights level up NR
  <![CDATA[ 
    if(getprop("controls/lighting/instruments-norm") < 1 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.2)
    }
  ]]>
U

Instrument lights level down NR
  <![CDATA[ 
    if(getprop("controls/lighting/instruments-norm") > 0 ) {
      setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.2)
    }
  ]]>
U

Step magnetos up NR
  controls.stepMagnetos(1)
U

Step magnetos down NR
  controls.stepMagnetos(-1)
U


If you find a line is too long for your page width, then you can press Enter and go to a new line. FG will be happy - it needs a tag or a ; at the end of a line to tell it that the current line of code is finished.

 setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)

becomes

 setprop("controls/engines/engine/mixture",
    getprop ("controls/engines/engine/mixture") + 0.05)

Notice where I put the newline and how I indented the second line. Both are designed to make it easy for people to read, as part of being a programmer is making your code legible for others.




Go back to Writing Joystick Code: Part 1.




Any complaints/suggestions/questions/kudos can be posted here.