Now while this was all nice and good so far, it turns out that this will maybe not be supported anymore after reworking the XML connection in Orxonox. Why? Because of the way we process Lua scripts in level files.
That's how it works so far:
- We have a level file with some Lua script in it (simplified XML code):
this code first includes another file, called "other_file.xml", then creates 5 randomly placed asteroids whose scale increases from 1 to 5.Code: Select all
<?lua include("other_file.xml") ?> <?lua for i = 1, 5 do ?> <Asteroid position="<?lua print(math.randomVector()) ?>" scale="<?lua print(i) ?>" /> <?lua end ?>
- The level file is loaded, but before XML parsing starts, our preprocessor removes all <?lua ?> tags and encloses the XML code in print("...") functions:
Code: Select all
include("other_file.xml") print(" ") for i = 1, 5 do print(" <Asteroid position=\"") print(math.randomVector()) print("\" scale=\"") print(i) print("\" />") end
- This Lua script is now executed by the Lua interpreter. The output (everything printed by the print() function plus everything included by the include() function) defines the final XML code:
Code: Select all
<some> <content /> <of /> <the> <included /> <file /> </the> </some> <Asteroid position="7,6,2" scale="1" /> <Asteroid position="2,2,9" scale="2" /> <Asteroid position="6,4,7" scale="3" /> <Asteroid position="9,6,3" scale="4" /> <Asteroid position="4,8,2" scale="5" />
- This XML code is then finally parsed by the XML parser.
At this point you probably understand why I'm so eager to get rid of the include() calls. And if you've read the previous posts, you know that there are different ways to replace them, so this is not a big deal.
However the future of the Lua code within level files is a bit unclear. If we ever create an editor, it will certainly replace a lot of functionality that was achieved through Lua code so far, for example to create lots of randomly placed asteroids - a simple script in the editor will do the job. Other scripting can be moved into script objects, i.e. BaseObjects that contain Lua code that will be executed if the object is triggered. This can be used for story scripting and to include stuff like the randomized space station.
Finally, there are even feasible solutions to write Lua scripts that can be saved back to XML, for example if we use a helper object that remembers the Lua code and also manages the objects that are created by this code.
Your thoughts about Lua code in XML files and possible solutions for the presented problems are of course very welcome