루아 바인드를 사용하고 있습니다.오히려 루아에서 객체를 생성합니다. 어떻게 루아가 직접 C++ 객체를 키워서 메소드를 시작하게할까요?
내 제목이 불분명 할 수도 있습니다. 최선을 다해서 질문하고 싶은 내용을 설명해 드리겠습니다.
내 질문은 :
Luabind에서 다른 개체를 만드는 대신 개체의 값 (특히 포인터)에 액세스 할 수있는 C++ 개체의 메서드를 직접 높이 키우려면 어떻게해야합니까?
내가 묻는 내용을 모른다면 계속 읽을 수 있습니다.
루아 Test_Class
만 Test_class
에서 생성 Main
, Test_Stage
:
예를 들어, 나는 3 개 종류가있다.
나는 테스트 목적으로 만 만들어진 변수 x
을 가지고 있습니다. 생성자를 통해 Main
에서 Test_Stage
에서 Test_Class
까지 계속 전달됩니다. 그래서 Test_Class
과 Test_Stage
은 내가 실제로 게임을 할 때 필요한 글로벌 값을 가지고 있습니다.
더 중요한 것은 Test_Class
이 의 포인터를 가지고 있으므로 create_a_bullet
또는 create_damage
과 같은 작업을 수행 할 수 있습니다. 이 Tutorial에서 배운
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()
{
}
"그리고 오류없이 IT 부서에서 아무것도 인쇄하지 못했습니다." 정말 오류가 없습니까? 특히'true'와'false'의 반환 값이 의미하는 것을 다시 읽으십시오 : http://stackoverflow.com/questions/21178454/luabind-did-not-launch-the-function-i-had-defined-to- 그것/21186858 # comment31911896_21186858 – Oberon
@Oberon 내가 메인 프로그램에서 아무런 의미가 없다는 구문 오류가 아니라는 것을 의미합니다 – MadokaMagica
'Test_Class'는 왜 루아 VM 생성을 처리합니까? 하나 이상의 lua 환경 (예 :'Test_Class' 인스턴스마다 새로운 별도의 루아 vm)을 갖고 자합니까? 또한'shoot_a_bullet'은 방법입니다. ***는 *** Test_Class 객체가 동작하기위한 문맥으로 필요하다는 것을 의미합니다. 어떤 질문을 던지면,''''''''''''''''''''''''''''''''''''''''''''''''''' – greatwolf