Jump to content
  • Advertisement
Sign in to follow this  

Using tolua++

This topic is 2635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys...I'm new to using tolua++. I'm having a problem in loading my C++ function using tolua++.
I followed this tutorial : Integrating ToLua++ and Your Game Engine : http://www.gamedev.n...me-engine-r2596
There are a couple of things I'm confused about.
1st of all : Say my header file's name is: myfile.h . So while creating the package file, do i need to include myfile.h?

2nd: After creating the package file and generating the cpp file, with what name do I call the function (that I exposed in my package file to lua) in my lua script .

For example: the function that I want to export is :

void yawWrapper(double angle);

In my tolua++ generated cpp file this function is mentioned as:

tolua_ScriptedScene_MinusLab_ScriptedScene_yawWrapper00(lua_State* lua_S)

ScriptedScene is the name of my class and MinusLab is the NameSpace

Share this post

Link to post
Share on other sites
1) You will want to include your header, myfile.h, in the package file using the slightly different syntax of:

$#include "myfile.h"

This syntax has the effect of inserting the line #include "myfile.h" into the generated .cpp file so that when the .cpp file is compiled, the declarations in myfile.h will be known. Otherwise, you end up with compile errors. The "$" prefix to the above just tells tolua to take the following line and copy it directly into the generated file. This syntax is useful as well if you need to include a using namespace whatever directive.

2) tolua creates a function called tolua_(package name)_open(lua_State *tolua_S), for which it generates a prototype at the very beginning of the generated .cpp file. In your program, you need to copy/paste this prototype to one of your source files, and call the indicated function with your lua_State. Once this is done, then from Lua script you could call yawWrapper as so:

yawWrapper(angle) -- call to a global function

ss=ScriptedScene() -- Instance a ScriptedScene using a constructor
ss:yawWrapper(angle) -- call the member function

ScriptedScene.yawWrapper(angle) -- call to a member function (static)

-- static functions of a class can also be called through instances of the class

MinusLab.ScriptedScene.yawWrapper(angle) -- call to a member function (static) of a class nested in a namespace

All of the "ugly" function names generated within the .cpp are for tolua++ internal use only, and are of absolutely no concern to you. The only one that you ever have to call directly is the open function. The others are automatically bound to userdata and functions in Lua, and the calling mechanism will call the appropriate bound functions as needed.

Share this post

Link to post
Share on other sites
I have 1 more question. How do I transfer class objects between C++ and lua. Are there different functions if we use tolua??
For ex: I have a Class Test:
Class Test {
public: int s;
void Add();
I have exposed the entire class to lua. All the functions using tolua++.
Now in my C++ code, I m creating a list of these objects. So right now I m creating a Test Object in my lua script as : tobj = Test:new()
So I want to give C++ this object and add it to the list. So I wanted to know how do I return this object to C++ and vice versa?

Share this post

Link to post
Share on other sites
I have always done it by providing a type of factory function in C++ that returns a pointer to the created object, and binding this function to call it from Lua. Or I will create the object in Lua and pass the created object to any C++ functions that need it.

Be warned that it can be slightly tricky to manipulate lists of objects if those lists must cross the boundary between Lua and C++. For example, if you want to create a std::list of objects, then in order to manipulate the list as a whole in Lua, you'll need to create a binding of std::list to Lua. And if you create the list as a Lua table, then it can be tricky getting that table into C++ without hand-writing a whole bunch of ugly code.

A solution to this comes from the fact that a Lua table will hold its integer-indexable members in an internal array rather than the key/value dictionary that named members go into. So if you create a list of objects like so:

for i=1,10,1 do

then those objects will be placed in the table objects, but will be managed internally as an array. And tolua++ provides a syntax for binding to arrays, so it is possible to create a C++ function that takes an array. The function must follow the parameter format of void func(int count, Type *array) or similar (ie, provide a size for the array before providing the array). The prototype in the package file then can be written as void func(int count, Type array[count]) and tolua++ will automatically generate the binding. Then you can call the function from Lua:

func(#objects, objects)

and the array will be correctly passed to func.

For myself, I try to stick to a system. If game objects are handled extensively in script, then I create them in script and hand them off to C++ functions as needed. (My usual method) But if I am creating a different structure, where scripts are just extensions of C++ objects and most of the functionality is done in C++, then I create factories and managers in C++ that can, if needed, be called by Lua to return a pointer to an object. Or I might harden the scripting environment even further, by setting up some sort of "script context", a script-accessible structure that holds certain pre-set members such as "current object", "target object", etc... and allow that as the only means for obtaining objects. But that is just an architectural choice, and I'm rambling a bit far afield here.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!