Food Industry (2.0 beta)

by Oceanel

[Warning (!): Mod is still under development, and is not intended for a long game] This mod adds hunger and ways to produce foods and drinks. From grown foods can be obtained effect capsules.

Content
3 years ago
0.16 - 1.1
1.25K
Fluids Manufacturing

b Feeds wrong calf

2 years ago

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...)

2 years ago
(updated 2 years ago)

Also I found something wonderful...
Its worthy of the first place in the absurd code contest.
Just look at files prototypes/scripts/[cattle,fish]/*_scripts.lua. All of them the same!

Q. What we do for avoid code dup?
A. Make abstract library.
Q. What they do for avoid abstraction?
A. Dup whole library for each object!!!

New response