当 Lua 绑定在该类中时,使用 LuaBind 调用类中的 Lua 函数

发布于 2024-12-05 03:48:10 字数 1369 浏览 3 评论 0原文

基本上,我只是希望能够在 Manager 类中创建一个干净的 Lua 实例,然后将类中的函数导出到 Lua,以便我可以在 Lua 中调用已创建的 C++ 类上的函数。

这是我目前正在考虑解决问题的方法。它可以编译,但 Lua 中没有任何反应。

有谁知道我做错了什么,或者有人有其他建议吗?

Manager.lua

newObject("Object", 1234)
printAll()

Manager.h

#ifndef MANAGER_H
#define MANAGER_H
#include <iostream>
#include <vector>
#include <string>

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "luabind/luabind.hpp"
#include "Object.h"


class Manager
{
private :
    lua_State *L;
    std::vector<Object> obj;


public :
    Manager();
    void newObject(std::string t, int nm);
    void printAll();

};

#endif

Manager.cpp

#include "Manager.h"
Manager::Manager()
{
luabind::open(L);

luabind::module(L) [
    luabind::class_<Manager>("Manager")
        .def(luabind::constructor<>())
        .def("newObject", &Manager::newObject)
    ];

luaL_dofile(L, "Manager.lua");
}   

void Manager::newObject(std::string t, int nm)
{
    if(t == "Object")
    {
        Object object(nm);
        obj.push_back(object);
    }
}

void Manager::printAll()
{
    for(unsigned int i = 0; i < obj.size(); i++)
        std::cout << obj[i].getNum() << std::endl;
}

Basically, I just want to be able to have a clean Lua instance made inside of my Manager class, then export the functions in the class to Lua, so that I can call functions on the already created C++ class inside of Lua.

This is the current way I am looking at solving the issue. It compiles but nothing happens in Lua.

Does anyone know what I am doing wrong, or does anyone have any other suggestions?

Manager.lua

newObject("Object", 1234)
printAll()

Manager.h

#ifndef MANAGER_H
#define MANAGER_H
#include <iostream>
#include <vector>
#include <string>

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "luabind/luabind.hpp"
#include "Object.h"


class Manager
{
private :
    lua_State *L;
    std::vector<Object> obj;


public :
    Manager();
    void newObject(std::string t, int nm);
    void printAll();

};

#endif

Manager.cpp

#include "Manager.h"
Manager::Manager()
{
luabind::open(L);

luabind::module(L) [
    luabind::class_<Manager>("Manager")
        .def(luabind::constructor<>())
        .def("newObject", &Manager::newObject)
    ];

luaL_dofile(L, "Manager.lua");
}   

void Manager::newObject(std::string t, int nm)
{
    if(t == "Object")
    {
        Object object(nm);
        obj.push_back(object);
    }
}

void Manager::printAll()
{
    for(unsigned int i = 0; i < obj.size(); i++)
        std::cout << obj[i].getNum() << std::endl;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2024-12-12 03:48:10

这样我就可以在 Lua 中已经创建的 C++ 类上调用函数。

如果您使用 Luabind 创建一个类,然后提供该类的成员,那么 Luabind 就会这样做。它将向 Lua 公开一个具有成员的类。

如果没有该类类型的对象,则无法在 C++ 中调用成员函数。因此,当您通过 Luabind 公开一个类及其成员时,如果没有该类类型的对象,您将无法调用 Lua 中的成员函数。

因此,如果你有一些全局 Manager 对象,将其公开给 Lua 的正确方法是将对象本身公开给 Lua。使用 Luabind 获取全局表,然后将指向 Manager 对象的指针放入其中。或者,您可以在执行脚本时将 Manager 对象实例作为参数传递。

第二种方法的工作方式如下:

//Load the script as a Lua chunk.
//This pushes the chunk onto the Lua stack as a function.
int errCode = luaL_loadfile(L, "Manager.lua");
//Check for errors.

//Get the function from the top of the stack as a Luabind object.
luabind::object compiledScript(luabind::from_stack(L, -1));

//Call the function through Luabind, passing the manager as the parameter.
luabind::call_function<void>(compiledScript, this);

//The function is still on the stack from the load call. Pop it.
lua_pop(L, 1);

您的 Lua 脚本可以使用 Lua 的 varargs 机制获取一个实例:

local manager = ...
manager:newObject("Object", 1234)
manager:printAll()

so that I can call functions on the already created C++ class inside of Lua.

If you use Luabind to create a class, and then provide members of that class, then Luabind will do exactly that. It will expose a class to Lua that has members.

You cannot call a member function in C++ without an object of that class's type. And therefore, when you expose a class and its members through Luabind, you will not be able to call member functions in Lua without an object of that class's type.

Therefore, if you have some global Manager object, the proper way to expose this to Lua is to expose the object itself to Lua. Use Luabind to get the global table, then put a pointer to your Manager object in it. Alternatively, you can pass the Manager object instance as a parameter when you execute the script.

The second method would work something like this:

//Load the script as a Lua chunk.
//This pushes the chunk onto the Lua stack as a function.
int errCode = luaL_loadfile(L, "Manager.lua");
//Check for errors.

//Get the function from the top of the stack as a Luabind object.
luabind::object compiledScript(luabind::from_stack(L, -1));

//Call the function through Luabind, passing the manager as the parameter.
luabind::call_function<void>(compiledScript, this);

//The function is still on the stack from the load call. Pop it.
lua_pop(L, 1);

Your Lua script can get an instance with Lua's varargs mechanism:

local manager = ...
manager:newObject("Object", 1234)
manager:printAll()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文