简体   繁体   English

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

[英]Use LuaBind to call Lua functions inside a class when Lua is bound inside THAT class

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. 基本上,我只希望能够在Manager类中创建一个干净的Lua实例,然后将该类中的函数导出到Lua,以便可以在Lua中已经创建的C ++类上调用函数。

This is the current way I am looking at solving the issue. 这是我正在寻求解决问题的当前方法。 It compiles but nothing happens in Lua. 它可以编译,但是在Lua中什么也没有发生。

Does anyone know what I am doing wrong, or does anyone have any other suggestions? 有人知道我在做什么错吗,或者有人有其他建议吗?

Manager.lua 经理

newObject("Object", 1234)
printAll()

Manager.h 经理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;
}

so that I can call functions on the already created C++ class inside of Lua. 这样我就可以在Lua内部已经创建的C ++类上调用函数。

If you use Luabind to create a class, and then provide members of that class, then Luabind will do exactly that. 如果您使用Luabind创建一个类,然后提供该类的成员,那么Luabind将会做到这一点。 It will expose a class to Lua that has members. 它将向具有成员的Lua公开一个类。

You cannot call a member function in C++ without an object of that class's type. 如果没有该类类型的对象,则无法在C ++中调用成员函数。 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. 因此,当您通过Luabind公开一个类及其成员时,如果没有该类类型的对象,您将无法在Lua中调用成员函数。

Therefore, if you have some global Manager object, the proper way to expose this to Lua is to expose the object itself to Lua. 因此,如果您有一些全局Manager对象,则将此对象公开给Lua的正确方法是将对象本身公开给Lua。 Use Luabind to get the global table, then put a pointer to your Manager object in it. 使用Luabind获取全局表,然后在其中放置指向您的Manager对象的指针。 Alternatively, you can pass the Manager object instance as a parameter when you execute the script. 或者,可以在执行脚本时将Manager对象实例作为参数传递。

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: 您的Lua脚本可以使用Lua的varargs机制获取实例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM