Nasal for C++ programmers: Difference between revisions

Jump to navigation Jump to search
m
http://flightgear.org/forums/viewtopic.php?f=30&t=20857&p=190385#p190385
m (http://flightgear.org/forums/viewtopic.php?f=30&t=20857&p=190385#p190385)
Line 1: Line 1:
{{Template:Nasal Navigation}}
{{Template:Nasal Navigation}}
If you are familiar with web browsers and JavaScript, then FlightGear scripting should be pretty straightforward:
Basically, Nasal scripting works exactly like JavaScript in your browser: Nasal is an embedded extension language, that is integrated into FlightGear as a conventional FlightGear subsystem (a so called "SGSubsystem") - during each frame, all subsystems, including the Nasal interpreter gets its time slice to process scripts - which is why badly written scripts may slow down the whole sim and affect frame rate/frame spacing.
Just imagine a bunch of FlightGear subsystems like these, which are all run sequentially:
* weather
* FDM
* I/O (Joystick, Pedals, Yoke)
* GUI
* Nasal Scripting Interpreter
Nasal itself has a standard library of general purpose functions, in addition it supports dozens of FG-specific extension functions to do various FG-related things, for example to interact with other subsystems (e.g. AI traffic, weather, FDM, GUI etc). In other words, while you'll find lots of standard library functions, there are many FG/simulation-specific functions available in FlightGear/Nasal.
An increasing number of subsystems in FlightGear no longer have direct scripting bindings via extension functions, but instead work by replicating their APIs on top of the property tree - so that these systems can be mostly controlled just by setting/getting properties - the autopilot system, AI Traffic and the canvas are some more recent examples of this.
Also, there are extension functions available which allow callbacks to be registered in the form of property listeners (which get invoked whenever a property is updated/written to) or via timers (that may expire and trigger a callback that gets called after expiration).
You will typically want to use listeners to get notifications whenever a certain property changes - a good example is a cockpit button or a GUI control that's updated. Timers are usually used to regularly run a piece of code, i.e. to do some form of background processing - stuff that doesn't need to be running all the same, but that needs to be run at regular intervals. Often times, callbacks invoked via timers will typically set up a new timer at the end of the routine, so that they basically invoke themselves. A more recent (and more sophisticated) option is the new "maketimer()" API.
Finally, there's a plethora of FlightGear-specific modules in $FG_ROOT/Nasal that greatly simplify coding stuff from scratch.


== Some words on Nasal for fellow C++ programmers ==
== Some words on Nasal for fellow C++ programmers ==

Navigation menu