Input device: Difference between revisions

Jump to navigation Jump to search
4,111 bytes added ,  19 October 2009
no edit summary
No edit summary
No edit summary
Line 355: Line 355:
     </script>
     </script>
   </nasal>
   </nasal>
==Perfiles ya creados fáciles de modificar==
===General tips===
* When testing a new xml file it is best to start FlightGear via a command window (rather than the GUI interface).  Any error messages will then be displayed in the terminal.  Error messages will give both a message and a line number, helping you pinpoint any errors.
* Errors can be detected on initial startup or at runtime.  Both types of errors will be displayed in the terminal.
* One of the most common errors is including a character that makes XML choke.  Such characters include
& < --
These characters will cause problems even if simply included in comments or within scripts.
* If your scripts contain any of these characters, you have to enclose the scripts in <script><![CDATA[...]]></script>. Alternatively, you can 'escape' the characters, ie "<" becomes "&lt;".
* Note that as of ver 1.9.1 there appears to be no way to tell FlightGear to reload joystick files at runtime.  So to test any changes to your file you must exit FlightGear and re-start, a somewhat time-consuming process.
* You can find many examples of different ways to program joysticks simply by examining the joystick xml files that are packaged with FlightGear.  See the directory FlightGear/data/input/joysticks
* Many advanced functions can be programmed using the Nasal scripting language.  These scripts are enclosed in <script></script> tags in the XML file. Helpful:
** A guide to the [[Nasal scripting language]] in FlightGear
** [[Nasal FAQ]]
** [[Howto: Write simple scripts in Nasal]]
* You can explore the internal property tree to see many variables that can be altered using joystick buttons or axes (File/Browse Internal Properties)
* You can test bits of Nasal code and do some other useful things using the Nasal Console (Debug/Nasal Console).
* All Nasal code shares a common namespace, so it's possible to set a variable in one nasal binding, and to read it in another.
===Useful hints for scripts===
Some particularly useful ideas for programming scripts within joystick XML files:
* getprop and setprop can be used for getting & setting properties from the internal properties tree:
var brake = !getprop("/controls/gear/brake-parking");
setprop("/controls/gear/brake-parking", brake);
* You can also make your own values on the property tree:
setprop("/input/joysticks/js[0]/myjoystick-modifier", 1);
var mod = getprop("/input/joysticks/js[0]/myjoystick-modifier");
* You can print to terminal using the print function. This is very useful for debugging.
print("Just", " a ", "test");
* You can display info in FlightGear via a popup.  This is useful for giving the user feedback about changes that may not be obvious via the panel.  It can also be useful for debugging.  Example:
gui.popupTip("Parking Brake ON");
Arguments for gui.popupTip must be strings, so if you want to display other types of variables they should be formatted with something like sprintf:
gui.popupTip(sprintf("Elevator trim: %d", 100 * getprop("/controls/flight/elevator-trim")));
Or
thv = getprop("/controls/engines/engine[0]/mixture");
gui.popupTip("Thrust vector " ~ int(thv * 120 - 20));
* You can just start using variables, ie,
x = 10;
But [http://wiki.flightgear.org/index.php/Nasal_scripting_language#Variables for various reasons] it is generally better to declare variables with the "var" statement:
var x = 10;
Note that "var" creates variables that are local in scope, which may cause problems if you are intending to use a variable globally among all different bindings in your joystick XML file.
* You can include a section of script that runs on startup to initialize variables, create functions, etc.  Example:
<PropertyList>
  <name type="string">My joystick name</name>
  <name type="string">My joystick name #2</name>
  <nasal>
    <script>
        #initialize variables
        f1 = f2 = 0;
        left_brake = right_brake = 0;
        # create a function to be used with all buttons
        getmod = func { getprop("/input/joysticks/js[0]/t-flight-hotas-x-modifier" ) }
    </script>
  </nasal>


==Resource==
==Resource==
* [http://www.flightgear.org/Docs/getstart/getstartch3.html#x8-360003.6 The FlightGear Manual]
* [http://www.flightgear.org/Docs/getstart/getstartch3.html#x8-360003.6 The FlightGear Manual]
5,775

edits

Navigation menu