【发布时间】:2015-01-30 16:28:22
【问题描述】:
我在我的代码中实现了一个观察者(或“侦听器”)模式:
struct EntityListener
{
public:
virtual void entityModified(Entity& e) = 0;
};
class Entity
{
public:
Entity();
void setListener(EntityListener* listener);
private:
EntityListener* m_listener;
};
现在,这在 C++ 中有效; Entity 类在需要时调用entityModified() 方法。现在,我想将一些功能转移到 Lua,在这些功能点中就是这个监听器回调。实体现在是从 Lua 脚本创建的。问题是,如何在 Lua 中实现监听功能?
例如,Lua 脚本当前执行如下操作:
function initializeEntity()
-- The entity object is actually created in C++ by the helper
Entity = Helper.createEntity()
-- Here I'd like to hook a Lua function as the Entity's listener
end
【问题讨论】: