# ToLua Tutorial

## Recommended Posts

Prozak    898
I'm currently trying to add a Lua binding wrapper to my project, and after testing LuaBind I would like to test toLua. Can anybody point me to a good tutorial on getting started with this tool?

##### Share on other sites
Prozak    898
One of the problems with all the Lua wrappers is that there just ins't enough info on the intertubes to implement those libs in our projects.

It's unfortunate. I would like to try toLua, but I think I'll just revert back to luaBind...

##### Share on other sites
Zorbfish    214
Well the problem is that for most Lua related projects the manual is treated as the definitive documentation. You didn't mention whether or not you had looked at the manual for toLua, but here it is for reference.

An updated version can be found as part of the toLua++ package here. Maybe this would be more useful because I think toLua++ is still actively being developed.

##### Share on other sites
Atridas    151
I've just readed the info in the reference you linked and I still have no idea how to use ToLua. I mean, i readed somethink like an "executable" but there is nothink ".exe" in the file you download...

Please answer to a very beginner level... I have a very good understanding of c/c++ language, but I still get freezed everytime a reference shoots me with source code and tells me "build it"

##### Share on other sites
jeroenb    282
I do not know of another tutorial then their documentation page Zorbfish already referenced. I found it good information and got me up and running with Tolua++.

What tolua basically does is converting your class prototypes (which you must manually create in a seperate file) to Lua tables which you later can use in your scripts. Thus say that you for example have the following class (the upper):

 // your real class definitionclass Character {public:   Character();   void setPosition(int x, int y);   int  getXPosition() const;   int  getYPosition() const;private:   int _x, _y;};// tolua class prototypeclass Character{   Character();   void setPosition(int x, int y);   int  getXPosition() const;   int  getYPosition() const;};

Then you must create the class prototype as the lower one. Then compile this prototype file with the tolua++ executable to a tolua library (see tolua docs). For example if you called your prototype file you can compile with the following line:

tolua++ -o tolua_character.cpp -H tolua_character.h -n character character.cpp

- tolua_character.cpp & tolua_character.h are the sources created by tolua
- character is the name of the tolue library
- character.cpp is the prototype file

You now can include the tolua_* files to your project. When you have created your Lua state call the following code (do not forget to include the tolua_character.h file):

tolua_open(luaState);tolua_character_open(luaState);

You now can create characters and call methods in your Lua scripts:

local char = Character:new()char:setPosition(5,5)

I hope this explanation helps you getting tolua running.