Exporting class function to lua

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

Recommended Posts

Hi, Im trying to export some functions from a class to lua but there seems to be some kind of problem that I do not understand. Heres a code snippet #include ".\LUA\lua.h" #include ".\LUA\lauxlib.h" #include ".\LUA\lualib.h" lua_State * mLuaState; class test { public: test() {} ~test() {} int testfunction (lua_State * tluastate) { return 1; } }; typedef int (test::*lua_CFunction)(lua_State *); void main (void) { test mytest(); lua_CFunction fp = test::testfunction; lua_register(mLuaState, "testfunc", fp); } Well.... I declared the function pointer like how the function pointer should be declared. I get an error like this: error C2373: 'lua_CFunction' : redefinition; different type modifiers error C2440: 'initializing' : cannot convert from 'int (__thiscall test::* )(lua_State *)' to 'lua_CFunction' None of the functions with this name in scope match the target type I do not understand why am I getting this error. I tried exporting a global function and it work but when i tried the above it doesn't. Is it that lua_register does not support exporting of class functions or did i miss out something that will make it work. PS: Im new with posting codes in this forums and would appreciate if someone can tell me how to encase codes within a tag. Thanks

Share on other sites
You cannot export a class function directly. Class functions are different from ordinary or static functions, they have a hidden parameter for the "this" pointer. If they are virtual then the situation is even more complicated (typically virtual function pointers have information about where the function is in the vtable ).

One way to get around this is to export a c function that reads something like a light user data ( i.e. a raw pointer ) from the lua stack and calls the function on it. There are probably different or better ways of doing this. Your program could crash if the lua script sends something other than a pointer of the correct type to the function.

For code tags, use [ source ] and [ /source ] without the spaces

Share on other sites
lua_CFunction fp = test::testfunction;
should be
lua_CFunction fp = &test::testfunction;
To get a C++-function pointer you need to use the 'address of'-operator. However, this will only work on static methods.

When you want to work with class instances, you should probably check out userdata. By using light user data, you can create (pointers to) objects and use lua_pushlightuserdata(L, newObject) to return it to Lua. Then you can do stuff like:
// Lualocal test = test:new()test:frozzleFooBar()// Cstatic int test_frozzleFooBar(lua_State *L) {    Test *test = (Test *)lua_touserdata(L, 1);    test->frozzleFooBar();    return 0;}
P.S. To post code, you can use [code] and [/code] as I did above, or for longer snippets [source] and [/source].
P.P.S. Mmm, beaten [smile].

Share on other sites
If you want to use your classes in lua I suggest that you have a look at libraries like tolua++ or luabind. These libraries will save you a lot of time exporting your classes to Lua.