Jump to content
  • Advertisement
Sign in to follow this  
faylar

Exporting class function to lua

This topic is 4311 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, 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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:
// Lua
local test = test:new()
test:frozzleFooBar()

// C
static 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 this post


Link to post
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!