| 40 | |
| 41 | == Using C/C++ functions in Lua with toLua++ == |
| 42 | |
| 43 | We 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 |
| 47 | With 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 | }}} |
| 56 | This is used to make the [wiki:Scripting#Levelfiles above] possible. |
| 57 | |
| 58 | === Mark the function and classes === |
| 59 | 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: |
| 60 | {{{ |
| 61 | ... |
| 62 | #include <list> |
| 63 | #include <string> |
| 64 | |
| 65 | // tolua_begin |
| 66 | namespace 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 | }}} |
| 80 | 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. |
| 81 | |
| 82 | 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. |
| 83 | |
| 84 | A function call in Lua looks by the way like this: |
| 85 | {{{ |
| 86 | local scr = orxonox.Script:getInstance() |
| 87 | scr: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 === |
| 92 | 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: |
| 93 | {{{ |
| 94 | tolua++ -n orxonox -H tolua_bind.h -o tolua_bind.cc tolua.pkg |
| 95 | }}} |
| 96 | It is necessary to exactly write this. Otherwise you will break the tolua_bind.h and .cc files and Orxonox will no longer compile. |