2014-01-20 22 views
0

我使用Luabind將我的Lua腳本綁定到我的C++引擎。 (它使用lua 5.1.4)當我輸入新文件時Luabind對象消失

我添加了一個名爲「controller.lua」的新的lua腳本,我的實體腳本,名爲「cat.lua」,將引用和使用。一個C++調用方法「更新」,這一切都在Lua手中。

但是,一旦我嘗試將綁定的C++方法傳遞給新的腳本文件,感覺就像來自該C++對象的所有綁定都會消失。我得到以下錯誤:

表達:腳本/ controller.lua:5(方法上移) 腳本/ controller.lua:5:試圖調用方法 'GetComponent'(一個零值)

這裏一些C++網頁摘要

// 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 

加分點: 當我做出改變控制器不工作(比如,如果我只是把一個性格任何地方),控制器負載高達零,沒有任何警告。有什麼方法可以使警告發出?

有沒有更好的方式來「鏈接」到其他lua文件,就像我使用Controller一樣?

回答

0

感謝ToxicFrog在Freenode上的聊天幫助我找到了答案。

基本上:我被呼叫控制器上移像這樣:

controller:MoveUp(entity) 

當然這轉化爲

controller.MoveUp(controller, entity) 

和功能被定義爲

function Controller.MoveUp(entity) 

這個 「實體」被接受作爲第一個參數,控制器,而實​​際的實體被丟棄每個規格。

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

相關問題