【问题标题】:Luabind objects disapearing when I enter a new file当我输入一个新文件时,Luabind 对象消失了
【发布时间】: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 的方式?

【问题讨论】:

    标签: c++ lua luabind


    【解决方案1】:

    感谢 Freenode 聊天中的 ToxicFrog 帮助我解决了这个问题。

    基本上:我是这样调用控制器 MoveUp 的:

    controller:MoveUp(entity)
    

    当然可以翻译成

    controller.MoveUp(controller, entity)
    

    函数被定义为

    function Controller.MoveUp(entity)
    

    这个“实体”被接受为第一个参数,控制器,而实​​际实体根据规范被丢弃。

    http://lua-users.org/wiki/ObjectOrientationTutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2019-09-27
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多