Cannot call member function from C ++ / lua function

I already tried something with lua and C ++, and all the "light" functions work, that is, they can call the C ++ function from lua and vice versa, however I have 1 main problem.

its a tiny game / simulator in the making, and I wanted to use lua, ode and make my own tiny interface (and not direct like LuaOde). My code structure is as follows

main class //creates instances of Visual, Physics and LuaAddons
  v = new Visual();
  p = new Physics();
  l = new LuaAddons();

Now, of course, I want to talk with physics from LuaAddons. so in LuaAddons.cpp I have:

int setWorldGravity(lua_State* L){
  float x = luaL_checknumber(L, 1);
  float y = luaL_checknumber(L, 2);
  float z = luaL_checknumber(L, 3);
  //Here I need to somehow talk to p (Physics())     
  return 0;
}

Thus, I can just call setWorldGravity(1, 2, 3)in my .lua file, and the above function is called accordingly with the correct parameters passed.

. int setWorldGravity , ODE .

, Physics.h :

void setWorldGravity(float x, float y, float z);

ODE :

void Physics::setWorldGravity( float x, float y, float z ){
  dWorldSetGravity (world_, x, y, z);  
} 

world_ - , , .

, int setWorldGravity(lua_State* L) LuaAddons (.. LuaAddons.h, ), (); . , LuaAddons , .

:

l = new LuaAddons(p);

( LuaAddons.h)

LuaAddons(Physics* p);

( LuaAddons.cpp)

LuaAddons(Physics* p){
  phys_ = p; // where phys_ is a private member of LuaAddons.h
}

,

phys_->setWorldGravity(x, y, z); // error C2065: 'phys_' : undeclared identifier

, ( ) .

Lunar.h. lua -, .h _- > . ( ) .lua, :

la_ = LuaAddons()
la_:setWorldGravity(1, 2, 3)

, . , ODE, ( .. - : P)

, lua, , . , .

, , .

/ .

+3
1

++ - . :

class Foo {
    const int bar_;

    public:
    Foo(int b) : bar_ { }
    int get_bar()
    {
        return bar_;
    }
};

Foo f(5);
f.get_bar();

, , :

Foo f = Foo_constructor(5);
Foo_get_bar(f);

++ this ( Python it self, ).

, , C- , ++, . - C, , -, : :

void setWorldGravityWrapper(Physics* p, int x, int y, int z)
{
    return p->setWorldGravity(x, y, z);
}

( ), .

+3

Source: https://habr.com/ru/post/1786546/


All Articles