Version 4 (modified by bknecht, 17 years ago) (diff) |
---|
Scripting
Orxonox uses 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 <?lua and ?> 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:
... <Model position="-9613,-4258,4537" scale="103" mesh="ast1.mesh" rotationAxis="0.820812,0.60695,0" rotationRate="13" /> <Model position="-6169,19264,-4878" scale="69" mesh="ast3.mesh" rotationAxis="0,0.585115,0.235653" rotationRate="32" /> <Model position="8717,-16685,9115" scale="37" mesh="ast4.mesh" rotationAxis="0.0512168,0.899599,0" rotationRate="15" /> ...
With the scripting engine we could generate those asteroids a lot easier:
<?lua for i = 1, 226, 1 do ?> <Model position="<?lua print(math.random(-19597, 18732))?>, <?lua print(math.random(-19597, 18732)) ?>, <?lua print(math.random(-19597, 18732)) ?>" scale="<?lua print(math.random( 20, 119)) ?>" mesh="ast<?lua print(i%6 + 1) ?>.mesh" rotationAxis="<?lua print(math.random()) ?>, <?lua print(math.random()) ?>, <?lua print(math.random()) ?> " rotationRate="<?lua print(math.random(16, 44)) ?>" /> <?lua end ?>
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 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:
- Update the tolua.pkg package file
- Mark the functions and classes you want to make accessible to Lua
- 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 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 <list> #include <string> // 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 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.