Error while running event FoodIndustry::on_tick (ID 0)
LuaEntity API call when LuaEntity was invalid.
stack traceback:
[C]: in function '__index'
...try__/prototypes/scripts/cattle/cattle_feeder_script.lua:54: in function 'cattle_feeder_process'
...try__/prototypes/scripts/cattle/cattle_feeder_script.lua:68: in function 'update'
...ry__/prototypes/scripts/cattle/cattle_feeders_script.lua:164: in function 'update_cattle_feeders'
...ry__/prototypes/scripts/cattle/cattle_feeders_script.lua:171: in function 'handler'
__core__/lualib/event_handler.lua:47: in function <__core__/lualib/event_handler.lua:45>
Why it's happened?
if ent.valid then
if inventory.get_item_count("fi-cattle-feed") > 0 then
local calf = self.cattle[entity.unit_number]
if calf == nil then
calf = { entity = ent, count = feedCalf }
self.cattle[entity.unit_number] = calf
end
calf.count = calf.count - 1
inventory.remove({name="fi-cattle-feed", count=1})
if calf.count <= 0 then
self.cattle[entity.unit_number] = nil
local calfPosition = calf.entity.position -- <-- HERE IS cattle_feeder_script.lua:54
calf.entity.destroy()
entity.surface.create_entity({ name="cattle", position=calfPosition, force=entity.force })
end
else
break
end
end
entity.unit_number
is a cattle feeder number, not a calf number. As result self.cattle[entity.unit_number] = calf
saves single calf and feeds it every time, instead of current calf. However, ent.valid
checks current calf, not saved calf. As result, game crashes if saved calf is dead.
My proposal: just use ent.unit_number
instead entity.unit_number
. Little memory leak better than crash (and we can make garbage collecting...)