[0.16.1]find_entities returns invalid in on_chunk_generated

Bugs that are actually features.
quyxkh
Smart Inserter
Smart Inserter
Posts: 1032
Joined: Sun May 08, 2016 9:01 am
Contact:

[0.16.1]find_entities returns invalid in on_chunk_generated

Post 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.
Rseding91
Factorio Staff
Factorio Staff
Posts: 16103
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.16.1]find_entities returns invalid in on_chunk_generated

Post 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.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Not a bug”