Input device: Difference between revisions

Jump to navigation Jump to search
→‎Resource: aadded more about programming tips
No edit summary
(→‎Resource: aadded more about programming tips)
Line 277: Line 277:


You should be able to at least get your joystick working along these lines. Concerning all the finer points, for instance, getting the joystick buttons working, John Check has written a very useful README being included in the base package to be found under '''FlightGear/Docs/Readme/Joystick.html'''. In case of any trouble with your input device, it is highly recommended to have a look into this document.
You should be able to at least get your joystick working along these lines. Concerning all the finer points, for instance, getting the joystick buttons working, John Check has written a very useful README being included in the base package to be found under '''FlightGear/Docs/Readme/Joystick.html'''. In case of any trouble with your input device, it is highly recommended to have a look into this document.
==More about programming joystick XML files==
===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.
* 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 exist 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.
* 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)
===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:
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")));
* 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 much better to declare them with the "var" statement:
var x = 10;


==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]
482

edits

Navigation menu