Howto:Nasal in scenery object XML files: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(Cleanup)
(a note on CDATA blocks in XML)
Line 29: Line 29:


Advice for testing: the object has to be present at sim startup. If you place an object with the UFO the xml-code gets executed, but not the Nasal code.
Advice for testing: the object has to be present at sim startup. If you place an object with the UFO the xml-code gets executed, but not the Nasal code.
To avoid problems with particular characters like < and >, consider [[XML#Data|using a CDATA block]]:
<syntaxhighlight lang="xml">
<nasal>
<load>
  <![CDATA[
    # ...
    # your nasal code here
    # ...
  ]]>
</load>
<nasal>
</syntaxhighlight>


[[Category:Nasal howto]]
[[Category:Nasal howto]]
[[Category:Scenery enhancement]]
[[Category:Scenery enhancement]]

Revision as of 15:11, 4 May 2014

FlightGear supports embedding Nasal code (scripts) into object XML files. This can be used for 'intelligent' objects -- such as for example special lighting features, specific lighthouse signals, animated hangar doors. In other words, things that couldn't be done with animations alone.

The code is run as soon as the tile loader decides to load the model. It can use listeners and timers and so run as long as it wishes. If the object is removed from memory, then a variable is changed in the object's namespace, so that the code can stop itself. An optional destructor part can be used for further cleanup (remove listeners etc.)

It is using two properies <load> and <unload>.

 <?xml version="1.0"?>
 <PropertyList>
        <path>helipad.ac</path>
        <nasal>
                <load>
                        loaded = 1;
                        print("Hello, I'm the helipad!");
                        var f = func {
                                if (!loaded) { return }
                                print("I'm still there!");
                                settimer(f, 5);
                        }
                        f();
                </load>
                <unload>
                        loaded = 0;
                        print("Bye, bye!")
                </unload>
        </nasal>
 </PropertyList>

Advice for testing: the object has to be present at sim startup. If you place an object with the UFO the xml-code gets executed, but not the Nasal code.

To avoid problems with particular characters like < and >, consider using a CDATA block:

<nasal>
 <load>
   <![CDATA[
     # ...
     # your nasal code here
     # ...
   ]]>
 </load>
<nasal>