Initializing Nasal early
This article describes content/features that may not yet be available in the latest stable version of FlightGear (2020.3). You may need to install some extra components, use the latest development (Git) version or even rebuild FlightGear from source, possibly from a custom topic branch using special build settings: .This feature is scheduled for FlightGear 4.x. If you'd like to learn more about getting your own ideas into FlightGear, check out Implementing new features for FlightGear. |
Warning: the information in this article is old and may be outdated. If you know about the internals of Nasal, please consider adding to Nasal Initialization
Started in | 07/2014 |
---|---|
Description | scripted startup, as per [1] and [2] |
Contributor(s) | Hooray, Philosopher (since 07/2014), |
Merge Request(s) | MR #42 (fgdata) Pending |
Status | Under active development as of 07/2014 [3] |
Topic branches: | |
$FG_SRC | https://gitorious.org/fg/philosophers-flightgear?p=fg:philosophers-flightgear.git;a=shortlog;h=refs/heads/topics/fgcanvas |
fgdata | https://gitorious.org/fg/canvas-hackers-fgdata?p=fg:canvas-hackers-fgdata.git;a=shortlog;h=refs/heads/topics/fgcanvas |
Portability & Compatibility |
---|
Performance |
Efforts |
Nasal internals |
---|
Memory Management (GC) |
I am delighted to have this cleaned up, and a way to incrementally init bits of Nasal |
I would prefer to init Nasal earlier in startup, because I would like use Canvas-based UI for pieces such as aircraft-selection - but right now we have to load Nasal in postInit, which is seconds and seconds into startup. Most Nasal things that need FDM state already wait on fdm-initialized, so in theory the only fix required is to skip <nasal> evaluation in 'animation' XML files (apparently there is a long-standing assumption that such elements are skipped when loading an aircraft as the main one, i.e they are only run for AI / MP aircraft - this is a bad situation but changing it now could be a serious compatibility problem)
|
The old ‘reset’, which does *not* restart Nasal, is now stripped down to the bare minimum of systems needed for repositioning (tele-porting). Since many aircraft Nasal scripts rely on /sim/signals/reinit to do work on being teleported, that behaviour was also retained.
In an ideal world, there would be a better API to inform aircraft and Nasal modules about a teleport, but for the meantime, the property you mentioned is the one. Eg, really we should have an API to distinguish on-ground vs in-air starts, and adjust the initial state of properties accordingly. BTW, for the new reset process, the subsystem, including Nasal, is actually destroyed and re-created. This is intentionally to make it almost certain the behaviour after a reset is the same as after the first launch of the sim. — James Turner (2014-12-10). Re: [Flightgear-devel] Expected behavior on "reinit".
(powered by Instant-Cquotes) |
The primary goals are:
Obviously supporting a standalone 'fgcanvas' would be quite a small extension from those. I'm not worrying about dynamic dependencies or automatic subsystem creation for the moment - I expect the user / defaults to have defined a set of subsystems that work without crashing You're correct of course that Nasal has many assumptions about subsystems, but I think that can be improved incrementally on the Nasal side. For the test mode, I really want to start Nasal-the-langauge very early, without loading all the modules in Nasal/ immediately. (Or maybe load a 'safe' subset). That's going to take some thought and I didn't get that far yet![1]— James Turner
|
This is exactly what I added support for implementing new commands in Nasal, so that going forward we can correctly put custom dynamic things in commands. |
on the complexity of dependencies for Nasal scripts, I think again the answer is to have the running status of each subsystem stored somewhere, and exposed via the property tree. The trick will be picking good groups, so most things have one or two dependencies, and having some documentation somewhere stating what's in each group. — James Turner (Apr 18th, 2006). Re: [Flightgear-devel] Subsystem run-levels.
(powered by Instant-Cquotes) |
the run-level concept is just one property, and nicely encapsulates the hierarchy of subsystems (if you're switching to level 8, all lower levels must already be initialied). But having names and separate flags makes the actual dependency clearer: 'this script depends on the aircraft subsystems' or 'this script depends on the environmental subsystems'. I guess it partly depends how complex the dependencies for any given script are in practice - hopefully they're actually quite simple. — James Turner (Apr 18th, 2006). Re: [Flightgear-devel] Re: Subsystem run-levels.
(powered by Instant-Cquotes) |
This is a summary of all discussions relating to:
- initializing the Nasal scripting interpreter earlier
- moving $FG_ROOT/Nasal initialization out of C++ into scripting space (aka "bootstrapping") while introducing proper dependency resolution (inter- and intra-modular)
- auditing $FG_ROOT for any other cases of embedded Nasal code that may have implicit assumptions regarding other subsystems (e.g. dialog files) Not done
- moving subsystem-specific initialization of Nasal APIs into each subsystem's bind methods, as per [5]
- extending cppbind to keep track of added symbols, to easily remove them on demand Pending
- moving subsystem-specific argument processing and fgcommand initialization into each subsystem Not done
- providing an integrated GUI front-end/launcher using Canvas and Nasal: Aircraft Center
- decoupling FlightGear initialization such that certain subsystems can be optionally disabled right at the beginning (e.g. FDM, sound, replay, scenery)
- simplifying the fg_init.cxx/main.cxx initialization logic (fgCreateSubsystems) and exposing it to scripting space
- allowing argument processing to be delegated to scripting space (e.g. options, startup position handling, presets etc.) Not done
- grouping related subsystems into SGSubsystemGroup instances to manage them holistically - analogous to run-levels, e.g.:
- AUDIO: sound, voice (TTS), fgcom
- AIRCRAFT: aircraft-model, controls, flight (FDM), xml-autopilot, route-manager
- VIDEO: gui, canvas, canvasGUI
- making the initialization process/splash screen more responsive by using Nasal/Canvas instead of hard-coded GL code [6] [7] (ticket #1456) Pending
- allowing custom startup profiles/modes to be implemented via Nasal helper scripts (which also touches the area of 'modding'), for example: Not done
- FGCanvas
- running stand-alone Canvas applications, e.g. the Interactive Nasal REPL or a MapStructure map
- FlightGear Benchmark
- FlightGear Headless
- FGViewer
- supporting stand-alone Nasal/Canvas-baased applications like a MapStructure-based moving map or a virtual ATC client
- supporting a safe subset of subsystems for mobile devices
Note The FlightGear initialization sequence is generally handled by $FG_SRC/Main/main.cxx, which is where subsystem initialization is delegated to fgCreateSubsystems() in $FG_SRC/Main/fg_init.cxx |
Objective
The position init code is a little complex, to allow for starting on a carrier and some other cases, and ideally we would do it from script, but unfortunately there's some technical limitations on doing that. (Not insurmountable, but not quick either). Adding more cases to the position-init code is certainly doable - one suggestion I made some time ago, is when MP is active, to default to starting at a free parking position instead of on a runway. (When no startup position is provided at all). This would avoid people accidentally starting on the runway in MP events, which would be a big usability win. When no parking positions are defined, AI traffic defaults to the 'airport centre' location. Selection a taxiway parallel to the active runway is possible, but for complex airport layouts I can imagine many ways this logic could fail. Personally I think it would be more robust to stick to picking parking positions, and otherwise using the airport centre, because it would encourage people to add the parking position data :) I anyone wants to work on this, it would be a self-contained function in position-init code, just ask here for any questions. All the pieces certainly exist - the taxiways, parking locations and runways for the airport are all accessible.[2]— James Turner
|
revisit bootstrapping soon, because it's the only sane way to resolve dependencies (intra-module or even subsystem support).
|
- ↑ James Turner (Oct 01, 2012). incremental initialization of SGSubsystems.
- ↑ James Turner (2013-11-12 10:32:27). Aircraft positioning on the runway.
For the sake of simplicity, we could refactor the ::init() method such that the common init code could be shared, between the initial startup Nasal interpreter, and the final runtime interpreter - so that we could load init code from a separate $FG_ROOT directory to do such things, without parsing all the stuff in $FG_ROOT/Nasal.
Referring to the GLSL segfault fix and my earlier comment about defaulting to a NON-SHADER environment by adjusting shader quality level to 0 if shaders are unsupported: For now, this is obviously easy to simply hard-code by showing an ALERT message and overriding the shader settings from there on.
However, in the long run, we'd probably want to simply delegate such stuff to Nasal space. While Nasal is currently not available that early, we could use a bare (or just temporary) Nasal instance during initialization to hand-off such stuff to scripting space. This seems to be an old idea actually (see the comments below from 2008) - and peopel were actually working on having Nasal + GUI code up really early back in 2008.
Given the state of the current init code in fg_init.cxx, the idea of factoring stuff out into Nasal space is very appealing - which means having less unmaintained C++ spaghetti code, and things like processing startup arguments can be really easily done from Nasal space, what's needed is "only" the props code - or rather just setprop/getprop, which would allow simple logic to be moved to scripting space - and it would not even be performance-critical.
In other words, it would be sensible to pick up the old idea and make a minimal Nasal interpreter available early on, and pass control to it - so that things like thís can be easily run, without hard-coding any logic:
if (getprop("/sim/rendering/gl-shading-language-version")=="UNSUPPORTED") {
print("Warning: Shaders unavailable, defaulting to quality level 0!\n")
setprop("/sim/rendering/shaders/quality-level", 0);
}
The only catch is, subsystems are initialised late, but I need a handful to be up before I can use the GUI dialogs; obviously the GUI subsystem itself, but also Nasal and a few others. (There are some issues with initialising nasal early, it is currently deliberately being done very late, but more on that later...)[1] — James Turner
|
I would appreciate if Nasal were around the first subsystems to be initialized. But this would require some Nasal dependency resolution. Almost all Nasal code needs globals.nas, and most need props.nas, too (which needs globals.nas). gui.nas needs globals.nas and props.nas, and screen.nas
needs all three etc. This needs to be taken care of if Nasal is to be initialized early.[2] — Melchior FRANZ
|
starting up the interpreter (the first part of FGNasalSys::init) can be done very early (and quickly), and the subsytem would then wait for a relatively high-valued 'init' call before running scripts (the part that needs all other properties to be defined).[3] — James Turner
|
I'm sure other people can propose potential groups, but I'd imagine:
early (nasal itself, GUI) time based subsystems (ephemeris, datetime, warp, properties, interpolator) environmental (weather fetch) aircraft systems (FDM, electrical, cockpit?, instruments, failures, GPS) scenery (tile manager, views, panels) input IO subsystems - ATC, traffic, multiplayer sound[4]— James Turner
|
I will start the systems I need (Nasal, GUI and input) early. This might also help make the startup progress a bit more fine-grained.[5] — James Turner
|
In the even longer run, we'd actually want to associate the Nasal scripts with run-levels (/etc/rc.d, anyone?), since the frontend GUI might want a few scripts loaded, while I assume most are only relevant when actually flying. Such a change also makes postinit() unnecessary, I think - since the effect can always be achieved by having init() watch for a higher run-level.[6] — James Turner
|
- ↑ James Turner (Mon, 17 Apr 2006 13:51:07 -0700). [Flightgear-devel] Subsystem run-levels.
- ↑ Melchior FRANZ (Tue, 18 Apr 2006 03:37:04 -0700). [Flightgear-devel] Re: Subsystem run-levels.
- ↑ James Turner (Mon, 17 Apr 2006 13:51:07 -0700). [Flightgear-devel] Subsystem run-levels.
- ↑ James Turner (Tue, 18 Apr 2006 08:23:01 -0700). Re: [Flightgear-devel] Subsystem run-levels.
- ↑ James Turner (Wed, 19 Apr 2006 06:43:14 -0700). [Flightgear-devel] Run-levels redux..
- ↑ James Turner (Mon, 17 Apr 2006 13:51:07 -0700). [Flightgear-devel] Subsystem run-levels.