2014-01-19 3 views
0

루아 바인드를 사용하고 있습니다.오히려 루아에서 객체를 생성합니다. 어떻게 루아가 직접 C++ 객체를 키워서 메소드를 시작하게할까요?

내 제목이 불분명 할 수도 있습니다. 최선을 다해서 질문하고 싶은 내용을 설명해 드리겠습니다.

내 질문은 :
Luabind에서 다른 개체를 만드는 대신 개체의 값 (특히 포인터)에 액세스 할 수있는 C++ 개체의 메서드를 직접 높이 키우려면 어떻게해야합니까?

내가 묻는 내용을 모른다면 계속 읽을 수 있습니다.

루아 Test_ClassTest_class에서 생성 Main, Test_Stage :

예를 들어, 나는 3 개 종류가있다.

나는 테스트 목적으로 만 만들어진 변수 x을 가지고 있습니다. 생성자를 통해 Main에서 Test_Stage에서 Test_Class까지 계속 전달됩니다. 그래서 Test_ClassTest_Stage은 내가 실제로 게임을 할 때 필요한 글로벌 값을 가지고 있습니다.
더 중요한 것은 Test_Class이 의 포인터를 가지고 있으므로 create_a_bullet 또는 create_damage과 같은 작업을 수행 할 수 있습니다. 이 Tutorial에서 배운

는, 나는 키 'Test_Stage'개체가 "Runned"< < '글로벌 가치'를 인쇄 할 것이다 Test_Class 호출 방법 shoot_a_bullet에서 만든 luaobject 만들기 위해 노력했다. C++에서 구문 오류가 발생하지 않았으므로 IT 부서에서 아무 것도 인쇄하지 못했습니다. 이 문제를 어떻게 해결할 수 있습니까?

코드가 여기에 있습니다 (사실은 내가 문제 클래스를 사용하여 포함하여 앞으로 작업, 그래서 "Runned"< < x는 Test_Class에 있었다. 그것은 글로벌 값이 여기에 통과했다 읽을 경우 적어도 내가 테스트 할 수 있습니다.)

편집 : 아무 것도하지 않고 몇 시간 만 지나면 다른 솔루션에 대한 생각이 들었습니다. 루아에서 생성 된 객체에서 사용할 수있는 포인터를 전달할 수있는 방법이 있습니까, 아니면 생성자에서 사용될 수 있습니까?

#include <iostream> 
#include "test_stage.h" 

using namespace std; 

int x; 

int main() { 

    cin >> x; 
    Test_Stage stage = Test_Stage(x); 
} 

그리고 Test_Stage의 헤더 :

#ifndef TEST_STAGE_H 
#define TEST_STAGE_H 
#include <iostream> 
class Test_Class;// to avoid circular include error, i used forward referancing 
       // i will include the file in the CPP file 
       // a class Test_Class which define the class is enough in header 
using namespace std; 

class Test_Stage 
{ 
    public: 
     int x; 
     Test_Stage(int num); 

     void create_bullet(int damage, string name, int x); /*This is currently useless 
      before i have understand how to include each other using foward referance*/ 
     void create_class(int num); 

     Test_Class t_class; 

     ~Test_Stage(); 
    private: 

}; 

#endif 

는 주요 파일이 프로그램을 시작

코드는 (당신은 당신이 질문에 대답을 위해 올 경우 주 및 Test_Stage를 건너 고려하고 있습니다

Test_Stage의 cpp 파일 :

#include"test_stage.h" 
#include "test_class.h"// and as you see i included both files(i just learned it few secs ago) 

Test_Stage::Test_Stage() 
{ 

} 

Test_Stage::Test_Stage(int num) 
{ 
    create_class(num); 
} 

void Test_Stage::create_bullet(int damage, string name, int x) 
{ 
    cout << "created damage: " << damage << "to" << x ; 
} 

void Test_Stage::create_class(int num) 
{ 
    Test_Class t_class = Test_Class(num, this); 
} 

Test_Stage::~Test_Stage() 
{ 

} 

Test_Class의 헤더 : 톤 후

#include "test_class.h" 
#include"test_stage.h" 

void Test_Class::shoot_a_bullet(Test_Class* o, int damage) 
{ 
    cout << "Runned"; 
    stage->create_bullet(damage, "wowo", x); 
} 

Test_Class::Test_Class() 
{ 

} 

Test_Class::Test_Class(int num, Test_Stage* stg) 
{ 
    stage = stg; 
    x = num; 
    // Create a new lua state 
     lua_State *myLuaState = luaL_newstate(); 

    // Connect LuaBind to this lua state 
    luabind::open(myLuaState); 
    luaL_openlibs(myLuaState); 


    luabind::module(myLuaState)[ 
     luabind::class_<Test_Class>("Test_Class") 
      .def("shoot_a_bullet", &Test_Class::shoot_a_bullet) 
    ]; 

    /*followed the tutorial codes 
    class_<A>("A") 
     .def("plus", &plus)*/ 

    cout << "im here";//just to check how far did the program go 
    luaL_dostring(
     myLuaState, 
     "shoot_a_bullet(134)\n" 
    ); 
    cout << "I passed it"; 
    cin.get(); 
    cin.get();//To pause the program before it closes 
    // if you have the time, can you also explain 
    // why do i need two cin.get() to pause the program. 
} 

Test_Class::~Test_Class() 
{ 

}  
+0

"그리고 오류없이 IT 부서에서 아무것도 인쇄하지 못했습니다." 정말 오류가 없습니까? 특히'true'와'false'의 반환 값이 의미하는 것을 다시 읽으십시오 : http://stackoverflow.com/questions/21178454/luabind-did-not-launch-the-function-i-had-defined-to- 그것/21186858 # comment31911896_21186858 – Oberon

+0

@Oberon 내가 메인 프로그램에서 아무런 의미가 없다는 구문 오류가 아니라는 것을 의미합니다 – MadokaMagica

+0

'Test_Class'는 왜 루아 VM 생성을 처리합니까? 하나 이상의 lua 환경 (예 :'Test_Class' 인스턴스마다 새로운 별도의 루아 vm)을 갖고 자합니까? 또한'shoot_a_bullet'은 방법입니다. ***는 *** Test_Class 객체가 동작하기위한 문맥으로 필요하다는 것을 의미합니다. 어떤 질문을 던지면,''''''''''''''''''''''''''''''''''''''''''''''''''' – greatwolf

답변

0

및 연구의 톤, 나는 마침내 해결책을 파악했다 :

#ifndef TEST_CLASS_H  
#define TEST_CLASS_H 
extern "C" 
{ 
#include <lua.h> 
    #include <lualib.h> 
    #include <lauxlib.h> 
} 
#include ".\luabind\luabind.hpp" 
#include <iostream> 
using namespace std; 

class Test_Stage; 

class Test_Class 
{ 
    public: 
     int x; 
     Test_Class(); 
     Test_Class(int num, Test_Stage* stage); 
     void shoot_a_bullet(Test_Class* o, int damage); 

     Test_Stage *stage; 
     ~Test_Class(); 
    private: 

}; 

#endif TEST_CLASS_H 

그리고 Test_Class의 마지막 CPP (나에게 많은의 문제를 일으키는) 그것이 얼마나 효율적인지 확신 할 수는 없지만, 내가 원하는 방식으로 실제로 작동하고 있습니다.
그래서 최종 해결책은 intptr_t, click here에 대한 자세한 내용 (주로 안전을 이유로 포인터 대신 int 디자인의 한 종류로 볼 수 있음)을 사용하여 포인터를 변환 한 intptr_t을 등록하는 것입니다. 나는 그것을 여기

을 사용하고 때와 코드 수정 후 정적 함수에있다 전달합니다

Test_Class.cpp 년 후 :

luabind::module(myLuaState)[ 
    .def("shoot_a_bullet", &shoot_a_bullet) 
];// since the functions are static now it will not need the namespace 

내가의 intptr_t 버전을 등록했다 포인터 :

//first convert pointer to intptr_t 
intptr_t stage = (intptr_t)stg;//stg is the pointer 

//then register it to lua 
//if you try to register a pointer it will give you runtime error 
luabind::globals(myLuaState)["stage"] = stage; 
우리는 또한 약간의 기능을 변경해야

:

//change the line defined the function to 
void static shoot_a_bullet(intptr_t stg, int damage); 

지금 우리가해야 할 : 우리는, 함수가 정적해야
먼저 헤더 파일 때문에 함수 그 자체로 가지 : 지금 그래서

void Test_Class::shoot_a_bullet(intptr_t stg, int damage) 
{ 
    //we need to convert it back to pointer first, so 
    Test_Stage* stage = (Test_Stage*)stg;//this is the part I'm not sure about efficiency 
    stage->create_bullet(damage, "wowo"); 
} 

코드 루아와 정말 간단합니다 : shoot_a_bullet (단, 134);

완료되었습니다. 인생은 좋습니다! 이 물건은 3 주간 비용이 들었습니다.

main 통화 당 :

#include <iostream> 
#include "test_stage.h" 

using namespace std; 

int x; 

int main() { 

cin >> x; 
cin.get();// clean the \n(when you press enter) after the number 
Test_Stage stage = Test_Stage(x); 
} 

Test_Stage

기회에 당신이 여기, 내가 말을 이해하지 않을 수 있습니다 것은 내가 writen했던 전체 테스트 코드입니다.시간 :

class Test_Class; 
#ifndef TEST_STAGE_H 
#define TEST_STAGE_H 
extern "C" 
{ 
    #include <lua.h> 
    #include <lualib.h> 
    #include <lauxlib.h> 
} 
#include ".\luabind\luabind.hpp" 
#include <iostream> 
#include "test_class.h" 
using namespace std; 

class Test_Stage 
{ 
    public: 
     int x; 
     Test_Stage(); 
     Test_Stage(int num); 

     void create_bullet(int damage, string name); /*This is currently useless 
     before i have understand how to include each other using foward referance*/ 
     void create_class(int num); 

     Test_Class t_class; 

     ~Test_Stage(); 
    private: 

}; 

#endif 

Test_Stage CPP :

#include"test_stage.h" 
#include "test_class.h"// and as you see i included both files 

Test_Stage::Test_Stage() 
{ 

} 

Test_Stage::Test_Stage(int num) 
{ 
    x = num;// a variable specific in this object(to test pointer) 
    create_class(num); 
} 

void Test_Stage::create_bullet(int damage, string name) 
{ 
    cout << "created damage: " << damage << " to " << x << endl;/*using the value 
    created in this object to see if pointer is actually working*/ 
} 

void Test_Stage::create_class(int num) 
{ 
    Test_Class t_class = Test_Class(this, num); 
} 

Test_Stage::~Test_Stage() 
{ 

} 

Test_Class .H :

#ifndef TEST_CLASS_H  
#define TEST_CLASS_H 
extern "C" 
{ 
#include <lua.h> 
    #include <lualib.h> 
    #include <lauxlib.h> 
} 
#include ".\luabind\luabind.hpp" 
#include <iostream> 
using namespace std; 

class Test_Stage; 

class Test_Class 
{ 
    public: 
     int x; 
     Test_Class(); 
     Test_Class(Test_Stage* stage, int num); 
     void static shoot_a_bullet(intptr_t stg, int damage); 

     ~Test_Class(); 
    private: 

}; 

#endif TEST_CLASS_H 

그리고 마지막으로, Test_Class 통화 당 :

#include "test_class.h" 
#include"test_stage.h" 

void Test_Class::shoot_a_bullet(intptr_t stg, int damage) 
{ 
    Test_Stage* stage = (Test_Stage*)stg; // intptr_t back to pointer 
    stage->create_bullet(damage, "wowo"); // use pointer 
} 

Test_Class::Test_Class() 
{ 

} 

Test_Class::Test_Class(Test_Stage* stg, int num) 
{ 
    intptr_t stage = (intptr_t)stg; 
    // Create a new lua state 
    lua_State *myLuaState = luaL_newstate(); 

    // Connect LuaBind to this lua state 
    luabind::open(myLuaState); 
    luaL_openlibs(myLuaState); 


    luabind::module(myLuaState)[ 
    luabind::def("shoot_a_bullet", &shoot_a_bullet)//again this is static now 
    ]; 

    luabind::globals(myLuaState)["stage"] = stage; 

    cout << "I'm here " << endl;//just to check how far did the program go 

    luaL_dostring(
     myLuaState, 
     "shoot_a_bullet(stage, 134)\n" 
    ); 

    cout << "I passed it"<< endl; 
    cin.get(); 
} 

Test_Class::~Test_Class() 
{ 

}  

재미를!

0

나는이 말을 알고하지만 당신은 당신을 반대 ++ 노출 C의 멤버 함수를 호출 할 경우, 당신은 왜 간단한 당신이 바로 첫 번째 (당신이 한있는 바인딩 모든 것을

luabind::module(myLuaState)[ 
    luabind::class_<Test_Class>("Test_Class") 
     .def("shoot_a_bullet", &Test_Class::shoot_a_bullet) 
]; 

luabind::globals(myLuaState)["stage"] = stage; 

luaL_dostring(
     myLuaState, 
     "stage::shoot_a_bullet(134)\n" 
    ); 

필요가 않았다 않습니다 didnt한다) 그리고 나서 당신은 객체를 (전역을 사용하는) 일부 루아 범위에 넣은 다음 객체 단계를 간단히 참조하고 :: myfunction을 사용하여 함수를 호출하십시오.