这让我疯了一个多星期.下面是来自游戏Bitfighter中机器人玩家的Lua代码的两个片段(用C++编写,使用LuaWrapper的变体进行绑定).
当我第一次启动Lua脚本时,两者都完全按预期工作.但经过几分钟的强烈对象创建和破坏后,变体2停止工作,并给我以下错误:
robot.lua:253: attempt to call missing or unknown method 'getEnergy' (a nil value)
Run Code Online (Sandbox Code Playgroud)
在我看来,这些功能应该相同.谁可以解释这个区别?
注意:target是表示C++对象的(重)用户数据.getEnergy和getHealth是正确注册的C++函数.我可以轻松地重现这种不同的行为.这是Lua 5.1,使用luavec mod.
变体1 - 始终有效
local mt = getmetatable(target)
local pow = mt.getEnergy(target) + mt.getHealth(target)
Run Code Online (Sandbox Code Playgroud)
变体2 - 在脚本运行任意时间后开始失败
local pow = target:getEnergy() + target:getHealth()
Run Code Online (Sandbox Code Playgroud)
要跟踪停止工作时发生的情况,您可以将调用包装起来pcall并探索target值发生的情况:
local ok, res = pcall(function() return target:getEnergy() + target:getHealth() end)
if not ok then
local s = "Invalid target value: "..tostring(target).." "..type(target).."\n"
for k, v in pairs(target) do s = s.."target "..tostring(k).." "..tostring(v).."\n" end
for k, v in pairs(getmetatable(target)) do s = s.."meta "..tostring(k).." "..tostring(v).."\n" end
-- add anything else that helps you figure out what happened to target
error(res..s)
end
local pow = res
Run Code Online (Sandbox Code Playgroud)