0

我已经从 C++ 导出到 lua 这样的基类:

class IState
{
    public:
        virtual ~IState() { }

        virtual void Init() = 0;
        virtual void Update(float dSeconds) = 0;
        virtual void Shutdown() = 0;
        virtual string Type() const = 0;
};

// Wraper of state base class for lua
struct IStateWrapper : IState, luabind::wrap_base
{
    virtual void Init() { call<void>("Init"); }
    virtual void Update(float dSeconds) { call<void>("Update", dSeconds); }
    virtual void Shutdown() { call<void>("Shutdown"); }
    virtual string Type() const { return call<string>("Type"); }
};

导出代码:

        class_<IState, IStateWrapper>("IState")
            .def("Init", &IState::Init)
            .def("Update", &IState::Update)
            .def("Shutdown", &IState::Shutdown)

下一部分:我有具有功能的 StateManager:void StateManager::Push(IState*)它是导出:

        class_<StateManager>("StateManager")
            .def("Push", &StateManager::Push)

现在,我想在 Lua 中创建 IState 类型的对象并将其推送到 StateManager 中:

-- Create a table for storing object of IState cpp class
MainState = {}

-- Implementatio of IState::Init pure virtual function
function MainState:Init()
    print 'This is Init function'
end

function MainState:Update()
    print 'this is update'
end

function MainState:Shutdown()
    print 'This is shutdown'
end

state = StateManager
state.Push(MainState)

当然,这是行不通的。如果 IState 类型的对象,我不知道该怎么说 MainState:

error: No matching overload found, candidates: void Push(StateManager&,IState*)

UPD

    module(state, "Scene") [
        class_<StateManager>("StateManager")
            .def("Push", &StateManager::Push),

        class_<IState, IStateWrapper>("IState")
            .def("Init", &IState::Init)
            .def("Update", &IState::Update)
            .def("Shutdown", &IState::Shutdown)
    ];

    globals(state)["StateManager"] = Root::Get().GetState(); // GetState() returns pointer to obj

示例后:

class 'MainState' (Scene.IState)
function MainState:__init()
    Scene.IState.__init(self, 'MainState')
end
...
state = StateManager
state:Push(MainState())

错误:“IState”类中没有静态“__init”

并且应该state = StateManager有括号?对于他们来说,没有这样的运营商是错误的。

4

1 回答 1

2

你不能只是向 Luabind 扔一张桌子。如果您打算从 Luabind 定义的类派生,则必须遵循 Luabind 的规则。您必须使用从您的类派生的Luabind 工具创建一个 LuaIState类。看起来像这样:

class 'MainState' (IState) --Assuming that you registered IState in the global table and not a Luabind module.

function MainState:__init()
    IState.__init(self, 'MainState')
end

function MainState:Init()
    print 'This is Init function'
end

function MainState:Update()
    print 'this is update'
end

function MainState:Shutdown()
    print 'This is shutdown'
end

state = StateManager()
state:Push(MainState())

另外,请注意对最后两行的更改。具体来说,StateManager 是如何被调用的,而不是简单地设置成state)。另外,你如何使用state:而不是state.. 我不知道这段代码在你的例子中是如何运作的。

于 2012-07-14T17:43:20.483 回答