Page 1 of 1

[0.16.1]find_entities returns invalid in on_chunk_generated

Posted: Wed Dec 13, 2017 10:35 pm
by quyxkh
In a scenario's control.lua I have

Code: Select all

on('chunk_generated', function(ev)
    for _, entity in next,ev.surface.find_entities(ev.area) or {} do
        if not ( entity.type == 'player'
           or keep_resources and entity.type == 'resource' ) then
            entity.destroy()
            end
        end
    --elided
    end)
and am now getting

Code: Select all

1084.699 Error MainLoop.cpp:1010: Exception at tick 0: Error while running event level::on_chunk_generated (ID 12)
LuaEntity API call when LuaEntity was invalid.
stack traceback:
        /home/quyxkh/.factorio/temp/currently-playing/control.lua:160: in function </home/quyxkh/.factorio/temp/currently-playing/control.lua:157>
where line 157 is `if not ( entity.type=='player'`. It seems wrong to me that `find_entities` would return invalid entities. The code worked in 0.15. I'd be happy with a workaround, I'm just not sure what that would be.

Re: [0.16.1]find_entities returns invalid in on_chunk_generated

Posted: Wed Dec 13, 2017 10:38 pm
by Rseding91
The call to find the entitites never returns invalid references however as you destroy the entities it can invalidate others so you still need to check if each is valid before you use it.

For example: destroying a cliff can cause other cliffs to be destroyed.