3 Replies - 2571 Views - Last Post: 02 May 2012 - 04:54 PM

#1 DennisJohnHarrop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-May 12

C++ and Lua problem

Posted 01 May 2012 - 12:09 PM

Hey </dream.in.code>,
I am trying to use lua in my game engine (but i thought id post it in c++ because it is not game programming related) I can get it to work except when Try and register an int(lua_State *L) that is in a class like
Class::int test(lua_State *L)
{
return 0;
}


it doesn't work

but when i do the int like this
int test(lua_State *L)
{
return 0;
}


it works but i cant access any of my variables in my class

my register code is in the same class and it looks like this:
lua_register(_luaMethods.get_state(), "test",test);



and i have tried this
  lua_register(_luaMethods.get_state(), "test",TimerMethods::test);


Thanks
Dennis

Is This A Good Question/Topic? 0
  • +

Replies To: C++ and Lua problem

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1617
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: C++ and Lua problem

Posted 01 May 2012 - 03:37 PM

Quote

but i thought id post it in c++

yes, that would have been the proper place for it but somehow it wound up in functional programing. a mod will move it later.

Class::int

what's that? that's not valid code right there; maybe you meant...
int Class::test(lua_State *L)
{
return 0;
}


but you also have to realize that this means 'Class::test' must be static.

you can't register a member function to Lua(you can fake it however). you can only register open functions(like your second one) or static functions. either way you can't access the members of a class.

to fake it you have to use a meta table. here, is a good example.

it does the following to create a meta table
luaL_newmetatable(L, "integer");
luaL_openlib(L, NULL, integermt, 0);

where the method names match up the the functions in 'integermt'. in C++ you are going to want to create a function that calls the destructor called "__gc" so that when the value needs to be collected the C++ object can clean it'self up.

it uses the following to create an instance of an integer
int* ud = lua_newuserdata(L, sizeof(int));
*ud = val;
luaL_getmetatable(L, "integer");
lua_setmetatable(L, -2);



Lua is a C library so it doesn't understand C++ classes; you have to create proxy functions that wrap the functionality of the class in something C can understand.

This post has been edited by ishkabible: 01 May 2012 - 03:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 DennisJohnHarrop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-May 12

Re: C++ and Lua problem

Posted 01 May 2012 - 10:43 PM

Sorry I meant
int Class::test(lua_State *L); 
but I was typing on my phone and messed up xD

Oh and I did post it in c++ first :S
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1617
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: C++ and Lua problem

Posted 02 May 2012 - 04:54 PM

well, you still can't use a class method; you can only use an open function or static class function. class methods are different form regular functions.

P.S. mod still needs to move this; I'll contact one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1