= Scripting =
Orxonox uses [http://www.lua.org Lua] for its scripts. Scripts are code pieces which add another layer to the game logic. As a Gamer you know those events which happen, when you reached a certain level or entered a room or area with your character. Those events are most certainly produced with a script engine like ours.
Our script engine has access to all objects in the game and are therefore quite powerful and easy to use, as you may see in examples.
There are two applications for the Orxonox scripting engine.
== Level files ==
Our level files are pure XML-code. We add objects and ambient lights with XML tags and specify their parameters with attributes.
If your using the special lua tags '''' you can create levels easier. You could use loops to generate more objects and you can let a level depend on earlier accomplishments of the player. If you use random parameters you can even create a level which looks slightly different every time a player plays it. Here some examples how you could use Lua in our level files.
=== Example: Asteroid field ===
As I write this wiki page we have a level with 226 asteroids in it. For each asteroid we need one line of XML code:
{{{
...
...
}}}
With the scripting engine we could generate those asteroids a lot easier:
{{{
}}}
This will randomly arrange asteroids of different look and rotation axis and speed in an area. In fact you can code any Lua you want according to the [http://www.lua.org/manual/5.1/manual.htm Lua-Manual]. To actually add something to the XML-output you can use the ''print'' function. The above example shows you best how this works.
== Using C/C++ functions in Lua with toLua++ ==
We use toLua++ for the transition from Orxonox to Lua. To use Lua for scripting there are three steps you have to do:
1. Update the tolua.pkg package file
2. Mark the functions and classes you want to make accessible to Lua
3. Auto-generate tolua_bind.h and tolua_bind.cc file
With those three steps Lua will be able to access the functions and classes you want to alter with the script.
=== Update the package file ===
!ToLua++ has a package file which we call ''tolua.pkg''. This file is used by toLua++ to generate the necessary C/C++ files. There are many things one can write in this package file. However we need only to include our own .h files here. The following line in the package file includes the .h file of the class ''Script'':
{{{
...
$cfile "Script.h"
...
}}}
This is used to make the [wiki:Scripting#Levelfiles above] possible.
=== Mark the function and classes ===
The included .h file will be parsed by toLua++. However we have to specify what exactly should be included. This is a shoted part of the Script.h file we justed included:
{{{
...
#include
#include
// tolua_begin
namespace orxonox
{
class Script
{
// tolua_end
public:
...
// some functions
void luaPrint(std::string str); // tolua_export
...
}; // tolua_export
} // tolua_export
}}}
As you see toLua++ parses only the party in the file where there is a ''tolua_exprt'' commentary and between ''tolua_begin'' and ''tolua_end'' commentaries. Like this we can specify exactly which functions should be accessible by Lua. You may want to only have Lua to fiddle with the public functions or you have a special private function just for Lua.
It is however important that you export namespaces and classes and not just the functions. Otherwise toLua++ would generate .h and .cc files which are not in the namespace so they cannot call the specified functions.
A function call in Lua looks by the way like this:
{{{
local scr = orxonox.Script:getInstance()
scr:luaPrint("Hello World")
}}}
''orxonox'' is the namespace, ''Script'' is the class and ''getInstance()'' and ''luaPrint()'' are the functions. ''scr'' is an instance of the Script-class.
=== Auto generate .h and .cc files ===
For this you will need tolua++ installed on your system. It can be downloaded [http://www.codenix.com/~tolua/#download here]. The following command will generate the necessary files to compile Orxonox after changes on the package file:
{{{
tolua++ -n orxonox -H tolua_bind.h -o tolua_bind.cc tolua.pkg
}}}
It is necessary to exactly write this. Otherwise you will break the tolua_bind.h and .cc files and Orxonox will no longer compile.