Jump to content
  • Advertisement
Sign in to follow this  
eruption_sword

another lua and C++ question

This topic is 3375 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

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
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

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
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!