Sign in to follow this  
eruption_sword

another lua and C++ question

Recommended Posts

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this