而是在Lua中创建对象,如何让Lua直接高C++对象启动方法?
我在用Luabind 我的标题可能有点不清楚,我会尽力解释我想问什么 我的问题是: 我如何直接使用CalthC++对象的方法来访问对象的值,特别是指针,而不是在Luabind中创建另一个对象。 如果你不知道我在问什么,你可以继续读下去 例如,我有三个类:Main、Test\u Stage、Test\u Class Lua仅在Test_类中创建 我有一个变量x,只是为了测试而创建的。它从主阶段一直传递到Test_阶段,再到Test_类,都是由它们的构造函数传递的。因此,Test_类和Test_阶段都有一个全局值,这是我在实际制作游戏时需要的。 更重要的是Test_类持有Test_Stage的指针,这样我就可以做一些事情,比如创建子弹或者创建伤害而是在Lua中创建对象,如何让Lua直接高C++对象启动方法?,c++,lua,luabind,C++,Lua,Luabind,我在用Luabind 我的标题可能有点不清楚,我会尽力解释我想问什么 我的问题是: 我如何直接使用CalthC++对象的方法来访问对象的值,特别是指针,而不是在Luabind中创建另一个对象。 如果你不知道我在问什么,你可以继续读下去 例如,我有三个类:Main、Test\u Stage、Test\u Class Lua仅在Test_类中创建 我有一个变量x,只是为了测试而创建的。它从主阶段一直传递到Test_阶段,再到Test_类,都是由它们的构造函数传递的。因此,Test_类和Test_阶段
从中了解到,我曾试图让Test_类中创建的luaobject调用shoot_a_bullet方法,该方法将使“Test_Stage”对象打印运行,经过无数次的研究,我终于找到了一个解决方案,尽管我不确定它的效率有多高,但它实际上按照我希望的方式工作。 所以最终的解决方案是,注册一个从我需要的指针转换成lua的intptr\t,有关intptr\t的更多信息,主要是出于安全原因,您可以将其视为一种只针对指针的int设计,并在我使用它时将其传递给静态函数 以下是修改后的代码: 在Test_Class.cpp中,在: 我已经注册了指针的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");
}
现在,lua的代码将非常简单:
射箭台,134
我们完了!生活是美好的!这东西花了我三周的时间
您可能不理解我说的话,这里是我编写的完整测试代码:
main.cpp:
测试阶段PP:
最后,测试_Class.cpp:
玩得开心 我知道现在很晚了,但你为什么不这么做呢
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"
);
你做的一切都是对的,如果你想调用一个被曝光的C++对象的成员函数,你首先需要绑定它,然后你把你的对象放进一些LUA范围,你使用全局,然后简单地引用对象阶段,调用一个函数::MyFrase
,没有错误,它没有打印任何东西。真的没有错吗?再看一次,特别阅读返回值true和false的含义:@Oberon i said no error意味着在主程序中没有语法错误,在luawh中没有语法错误Test_类为什么处理lua VM创建?您是否打算拥有多个lua环境,例如为每个测试类实例提供一个新的单独的lua vm?此外,射出子弹也是一种方法。这意味着它需要一个Test_类对象作为它工作的上下文。这就引出了一个问题,你打算在你的luaL_dostring中拍摄什么样的场景?你的双重cin.get调用可能是因为缓冲区中仍然有一个\n字符-可能是从你在main中的第一个cin开始的。@greatwolf首先,你说得对,我想要不止一个lua环境,因为我希望我的游戏有很多自由,其他人可以为它编写新的东西,比如mugan,第二,我想在我创建lua VM的对象实例中运行shot_a_bullet,因为我需要它来调用stage类,在实际使用中,它将添加bulletdamage类的实例,该实例还可以与舞台的显示交互并在窗口上绘制#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()
{
}
luabind::module(myLuaState)[
.def("shoot_a_bullet", &shoot_a_bullet)
];// since the functions are static now it will not need the namespace
//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");
}
#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);
}
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
#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()
{
}
#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
#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()
{
}
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"
);