another lua and C++ question

Started by
1 comment, last by apefish 14 years, 8 months ago
I am running into some problems using Lua and C++ together. I have a class called battle, inside which I have a lua_State* variable. I wrote some functions which I can then call from a Lua file. However, the problem is that inside these functions I want to be able to access the member variables inside the battle class. It seems I can't do that because the lua functions must be static. Is there any way around this? Thanks. Example: In battle.h I have: static int lua_skillIsNamed(lua_State *L); In battle.cpp I have: int battle::lua_skillIsNamed(lua_State *L) { std::string name = lua_tostring(L,1); if (name == mAction.name) lua_pushnumber(L, 1); else lua_pushnumber(L, 0); return 1; } This doesn't work because mAction is a member variable of the battle class.
Advertisement
You'll have to use a heavy duty binding library like MLuabind or Luabind which can bind classes for you. Or if you know enough about the Lua C API u can use what they call light user data and push around the class instance within the call stack and pop it off when u need to access a class specific instance variable.

I suggest using a binding library.

Good Luck!

-ddn
presumably if your static function can get it's hands on an instance of your class, it can call methods on it to get at the state.

I don't know C++ (I use C) but isn't there some kind of "friend" construct that allows you to directly get at the state of your object? You still would have to write self.mAction.name or whatever.

Any binding libraries you use just end up using the same Lua API so it shouldn't be too much trouble to do it yourself. Just be creative.

I hope that helps.

This topic is closed to new replies.

Advertisement