Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Version 3 and Version 4 of code/Scripting


Ignore:
Timestamp:
Apr 14, 2008, 12:49:58 AM (17 years ago)
Author:
bknecht
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/Scripting

    v3 v4  
    3838}}}
    3939This 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.
     40
     41== Using C/C++ functions in Lua with toLua++ ==
     42
     43We use toLua++ for the transition from Orxonox to Lua. To use Lua for scripting there are three steps you have to do:
     44 1. Update the tolua.pkg package file
     45 2. Mark the functions and classes you want to make accessible to Lua
     46 3. Auto-generate tolua_bind.h and tolua_bind.cc file
     47With those three steps Lua will be able to access the functions and classes you want to alter with the script.
     48
     49=== Update the package file ===
     50!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'':
     51{{{
     52...
     53$cfile "Script.h"
     54...
     55}}}
     56This is used to make the [wiki:Scripting#Levelfiles above] possible.
     57
     58=== Mark the function and classes ===
     59The 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:
     60{{{
     61...
     62#include <list>
     63#include <string>
     64
     65// tolua_begin
     66namespace orxonox
     67{
     68
     69  class Script
     70  {
     71    // tolua_end
     72    public:
     73...
     74// some functions
     75    void luaPrint(std::string str); // tolua_export
     76...
     77  }; // tolua_export
     78} // tolua_export
     79}}}
     80As 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.
     81
     82It 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.
     83
     84A function call in Lua looks by the way like this:
     85{{{
     86local scr = orxonox.Script:getInstance()
     87scr:luaPrint("Hello World")
     88}}}
     89''orxonox'' is the namespace, ''Script'' is the class and ''getInstance()'' and ''luaPrint()'' are the functions. ''scr'' is an instance of the Script-class.
     90
     91=== Auto generate .h and .cc files ===
     92For 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:
     93{{{
     94tolua++ -n orxonox -H tolua_bind.h -o tolua_bind.cc tolua.pkg
     95}}}
     96It is necessary to exactly write this. Otherwise you will break the tolua_bind.h and .cc files and Orxonox will no longer compile.