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

From FlightGear wiki
Jump to navigation Jump to search
m (Categories)
No edit summary
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Stub}}
{{Nasal Navigation}}
For introductory information about Nasal, you'll want to check out [[Nasal scripting language]].
[[FlightGear]] supports '''embedding [[Nasal]] scripts into scenery object XML files'''. This can be used for "intelligent" objects, such as special lighting features, specific lighthouse signals, and animated hangar doors. In other words, things that couldn't be done with [[Howto:Animate models|animations]] alone.  


(As of 05/2009, this is work in progress and entirely based on [http://www.mail-archive.com/flightgear-devel@lists.sourceforge.net/msg01757.html]).
In the example below, the code in the <syntaxhighlight lang="xml" inline><load></syntaxhighlight> tag is run as soon as the tile loader decides to load the model. Using a {{func link|maketimer()}}, it prints a message every 5 seconds. When the object is removed from the memory, the code within the <syntaxhighlight lang="xml" inline><unload></syntaxhighlight> tag is executed. The timer is stopped and a message is printed. The code in the <syntaxhighlight lang="xml" inline><unload></syntaxhighlight> tag may also do things like [[Nasal library#removelistener.28.29|removing listeners]].


There's now support for embedding Nasal code (scripts) into object XML files.
{{warning| In dialogs you have to use <open> and <close> instead of <load> and <unload>}}


This can be used for 'intelligent' objects -- such as for example special lighting features, specific lighthouse signals, things that couldn't be done with animations alone.
<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="UTF-8"?>


The code is run as soon as the tile loader decided 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.)
<PropertyList>


It is using two properies <load> and <unload>.
<path>helipad.ac</path>
Example:
<nasal>
  <load><![CDATA[
    print("Hello, I'm the helipad!");
    var timer = maketimer(5, func(){
        print("I'm still here!");
    });
    timer.start();
  ]]></load>


<?xml version="1.0"?>
  <unload><![CDATA[
<PropertyList>
    timer.stop();
        <path>helipad.ac</path>
    print("Bye, bye!")
        <nasal>
  ]]></unload>
                <load>
</nasal>
                        loaded = 1;
 
                        print("Hello, I'm the helipad!");
</PropertyList>
                        var f = func {
</syntaxhighlight>
                                if (!loaded) { return }
 
                                print("I'm still there!");
{{note|The object has to be present at sim startup. If you place an object with the UFO, the XML code will be executed, but not the Nasal code.}}
                                settimer(f, 5);
 
                        }
{{note|It is best practice to put Nasal code in a [[XML#Data|CDATA section]]. This prevents problems with the Nasal code containing {{wikipedia|List of XML and HTML character entity references#Predefined entities in XML|predefined XML entities}}.
                        f();
<syntaxhighlight lang="xml">
                </load>
<nasal>
                <unload>
  <load><![CDATA[
                        loaded = 0;
    # ...
                        print("Bye, bye!")
    # your nasal code here
                </unload>
    # ...
        </nasal>
  ]]></load>
        <animation>
<nasal>
                <type>noshadow</type>
</syntaxhighlight>
                <object-name>helipad</object-name>
}}
        </animation>
</PropertyList>


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

Latest revision as of 14:51, 24 April 2020

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

In the example below, the code in the <load> tag is run as soon as the tile loader decides to load the model. Using a maketimer() , it prints a message every 5 seconds. When the object is removed from the memory, the code within the <unload> tag is executed. The timer is stopped and a message is printed. The code in the <unload> tag may also do things like removing listeners.

Warning  In dialogs you have to use <open> and <close> instead of <load> and <unload>
<?xml version="1.0" encoding="UTF-8"?>

<PropertyList>

<path>helipad.ac</path>
<nasal>
  <load><![CDATA[
    print("Hello, I'm the helipad!");
    var timer = maketimer(5, func(){
        print("I'm still here!");
    });
    timer.start();
  ]]></load>

  <unload><![CDATA[
    timer.stop();
    print("Bye, bye!")
  ]]></unload>
</nasal>

</PropertyList>
Note  The object has to be present at sim startup. If you place an object with the UFO, the XML code will be executed, but not the Nasal code.
Note  It is best practice to put Nasal code in a CDATA section. This prevents problems with the Nasal code containing predefined XML entities This is a link to a Wikipedia article.
<nasal>
  <load><![CDATA[
    # ...
    # your nasal code here
    # ...
   ]]></load>
<nasal>