在游戏中运行一段时间后,模组在 control.lua 的 upgrade_entity_quality 函数中崩溃。
错误信息:
Error while running event ai_cn::on_nth_tick(6)
LuaEntity doesn't contain key kill_count.
stack traceback:
[C]: in function 'index'
__ai_cn/control.lua:123: in function 'upgrade_entity_quality'
ai_cn/control.lua:187: in function 'get_buildings'
ai_cn/control.lua:239: in function <ai_cn/control.lua:231>
原因分析:
control.lua 第 121-146 行处理 turret(炮塔)品质升级时,直接访问了 entity.kill_count。
Factorio 2.0 中 LuaEntity 已移除 kill_count 属性,访问时触发 __index 元方法报错。
受影响的代码位置:第 123 行(读取)、第 127 行(读取)、第 143 行(写入)。
建议修复:
对 kill_count 的读写使用 pcall 包裹,对于没有该属性的炮塔实体跳过品质升级:
local ok, kill_count = pcall(function() return entity.kill_count end)
if not ok or kill_count <= threshold then return false end
手动修复后解决了该问题
修改内容:
-- 第123行:读取改为 pcall
local ok, kill_count = pcall(function() return entity.kill_count end)
if not ok or kill_count <= threshold then
return false
end
-- 删除原来的 local kill_count = entity.kill_count(已在pcall中获取)
-- 第143行:写入也加 pcall 保护
pcall(function() upgraded.kill_count = kill_count end)