Modules.nas: Difference between revisions

Jump to navigation Jump to search
2,359 bytes added ,  24 February 2020
Line 152: Line 152:


== HowTo / Examples ==
== HowTo / Examples ==
to be written
 
=== Nasal modules in an aircraft (e.g. for development) ===
Nasal code for an aircraft is usually loaded by a XML declaration in the <aircraft>-set.xml file like this:
<syntaxhighlight lang="nasal">
<PropertyList>
    <nasal>
        <foo> <!-- Nasal namespace foo -->
            <file>Nasal/foo.nas</file>
        </foo>
        <bar> <!-- Nasal namespace bar -->
            <file>Aircraft/Generic/foo.nas</file>
        </bar>
        <myAircraft>
            <file>Nasal/efis_module.nas</file>
        </myAircraft>
    </nasal>
</PropertyList>
</syntaxhighlight>
 
This is fine unless you are developing a Nasal subsystem for the aircraft and have to restart FlightGear every time you edit a few lines.
For this case you could consider to use the Module class (defined in FGDATA/Nasal/modules.nas) and make your code re-loadable at runtime,
which means you do not have to restart whole FlightGear just for a few lines of edited Nasal code.
 
'''Example Nasal/efis_module.nas:'''
<syntaxhighlight lang="nasal">
#-- load EFIS as reloadable module
var my_efis = modules.Module.new("myAircraft_EFIS"); # Module name
my_efis.setDebug(1); # 0=(mostly) silent; 1=print setlistener and maketimer calls to console; 2=print also each listener hit, be very careful with this!
my_efis.setFilePath(getprop("/sim/aircraft-dir")~"/Nasal/EFIS");
my_efis.setMainFile("myAircraft-efis.nas");
my_efis.load();
</syntaxhighlight>
 
Now add a menu item for easy reloading like this:
{{note|
The module name in the property path must match the name passed to modules.Module.new()
|margin=10px |width=50%}}
<syntaxhighlight lang="xml">
<menubar>
    <default>
        <menu n=100>
            <!-- normal aircraft menu -->
        </menu>
        <menu n=101>
            <label>Aircraft Development</label>
            <item>
                <label>Reload EFIS</label>
                <binding>
                    <command>property-assign</command>
                    <property>/nasal/modules/myAircraft_EFIS/reload</property>
                    <value>1</value>
                </binding>
            </item>
        </menu>
    </default>
</menubar>
</syntaxhighlight>
 
{{tip|
You can also call {{code| myAircraft.my_efis.reload();}} to trigger the reload. myAircraft is the namespace given in the -set.xml file, my_efis is the module object (see above).
|margin=10px | width=50%}}


== Existing modules ==
== Existing modules ==
252

edits

Navigation menu