Howto:Nasal in scenery object XML files: Difference between revisions
Jump to navigation
Jump to search
(a note on CDATA blocks in XML) |
Red Leader (talk | contribs) (Cleanup) |
||
Line 1: | Line 1: | ||
[[FlightGear]] supports '''embedding [[Nasal]] | {{Nasal Navigation}} | ||
[[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. | |||
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]]. | |||
<syntaxhighlight lang="xml"> | |||
<?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> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
{{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 [[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}}. | |||
<syntaxhighlight lang="xml"> | <syntaxhighlight lang="xml"> | ||
<nasal> | <nasal> | ||
<load><![CDATA[ | |||
# ... | |||
# your nasal code here | |||
# ... | |||
]]></load> | |||
]]> | |||
<nasal> | <nasal> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
}} | |||
[[Category:Nasal howto]] | [[Category:Nasal howto]] | ||
[[Category:Scenery enhancement]] | [[Category:Scenery enhancement]] |
Revision as of 09:03, 7 April 2016
The FlightGear forum has a subforum related to: Nasal Scripting |
Nasal scripting |
---|
Nasal internals |
---|
Memory Management (GC) |
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.
<?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 .
<nasal>
<load><![CDATA[
# ...
# your nasal code here
# ...
]]></load>
<nasal>
|