【发布时间】:2014-01-22 10:16:47
【问题描述】:
我正在使用Luabind 将我的 Lua 脚本绑定到我的 C++ 引擎。 (它使用 lua 5.1.4)
我添加了一个名为“controller.lua”的新 lua 脚本,我的实体脚本“cat.lua”将引用和使用它。一个 c++ 调用方法“更新”它全部在 Lua 手中。
但是一旦我尝试将绑定的 c++ 方法传递给新的脚本文件,感觉就像来自该 c++ 对象的所有绑定都消失了。我收到以下错误:
表达式:scripts/controller.lua:5(方法MoveUp) scripts/controller.lua:5: 尝试调用方法 'GetComponent'(一个 nil 值)
这是一些 C++ sn-ps
// Definitions
module(luaState)
[
class_<Entity>("Entity")
.def("GetComponent", &Entity::GetComponent)
class_<Component>("Component")
.enum_("eComponentTypes")
[
value("Steering", kComponentType_Steering)
],
class_<SteeringComponent>("SteeringComponent")
];
// The script components update
void ScriptComponent::Update() {
const Entity* owner = this.GetOwner();
mLuaDataTable["Update"](owner); // Executes the Update function on the script Cat.lua
}
c++ 调用的实体代码(执行时会将 Cat 表返回给 c++。)
-- Cat.lua
local controller = loadfile("scripts/controller.lua")
local Cat = {}
function Cat.Update(entity)
steeringComponent = entity:GetComponent(Component.Steering) -- Works fine
controller:MoveUp(entity)
end
return Cat
和控制器
--controller.lua
local up = vec2(0.0, 1.0)
local Controller = {}
function Controller.MoveUp(entity)
steeringComponent = entity:GetComponent(Component.Steering) -- Fails
end
return Controller
奖励积分: 当我对不起作用的控制器进行更改时(比如我只是在任何地方扔了一个 s 字符),控制器加载为零,没有警告。有什么办法让它抛出警告吗?
有没有更好的方法来“链接”到其他 lua 文件,比如我使用 Controller 的方式?
【问题讨论】: