Page 1 of 1

(Resolved) lua error when trying to destroy a corpse entity

Posted: Fri Jul 08, 2016 6:05 am
by devilwarriors
Hi,

when I try to remove a corpse entity on the "on_trigger_created_entity" event it trow this error

Code: Select all

LuaEntity API call when LuaEntity was invalid
what I'm a doind wrong? is it possible to destroy an entity of type corpse?

the entity :

Code: Select all

data:extend({
  {
    type = "corpse",
    name = "extinguisher-remnants",
    icon = "__base__/graphics/icons/remnants.png",
    flags = {"placeable-neutral", "not-on-map"},
    selection_box = {{-0.5, -0.5}, {0.5, 0.5}},
    tile_width = 1,
    tile_height = 1,
    selectable_in_game = false,
    subgroup = "remnants",
    order="d[remnants]-a[generic]-a[small]",
    time_before_removed = 60 * 60 * 5, -- 15 minutes
    final_render_layer = "lower-object",
    animation =
    {
      [...]
    }
  },
})
The control.lua file

Code: Select all

local function extinguish_fire(event)
  if event.entity.name == 'extinguisher-remnants' then
    local position = event.entity.position;
    local area_start = {position.x - 1, position.y - 1}
    local area_end = {position.x + 1, position.y + 1}

    for _, entity in pairs(game.surfaces['nauvis'].find_entities{area_start, area_end}) do
      if entity.type == 'fire' then
        entity.destroy()
      end

      if entity.type == 'transport-belt' then
        event.entity.destroy() -- THIS CAUSE THE CRASH
      end
    end
  end
end

script.on_event(defines.events.on_trigger_created_entity, extinguish_fire)

So basically I want the corpse to remove itself if it spawn on a transport-belt

Thanks!

Re: lua error when trying to destroy a corpse entity

Posted: Fri Jul 08, 2016 2:39 pm
by DedlySpyder
You need to check the entity.valid to see if you can affect it.

Re: lua error when trying to destroy a corpse entity

Posted: Fri Jul 08, 2016 7:46 pm
by devilwarriors
DedlySpyder wrote:You need to check the entity.valid to see if you can affect it.
I tried that, I get the same error. make no sense :\

Code: Select all

      if entity.type == 'transport-belt' and event.entity.valid then
        event.entity.destroy() -- THIS CAUSE THE CRASH
      end

Re: lua error when trying to destroy a corpse entity

Posted: Fri Jul 08, 2016 7:52 pm
by devilwarriors
The weird part is if I don't put a .valid check the error happen on line 5

Code: Select all

      if entity.type == 'fire' then
        entity.destroy()
      end

      if entity.type == 'transport-belt' then
        event.entity.destroy() -- THIS CAUSE THE CRASH
      end
but if I add the check, the error move to line 2 in the error message, but of course if I get rid of event.entity.destroy() the code at line 2 is fine.

Code: Select all

      if entity.type == 'fire' then
        entity.destroy() -- NOW THIS CAUSE THE CRASH.. wtf
      end

      if entity.type == 'transport-belt' and event.entity.valid then
        event.entity.destroy() 
      end
what's going on..

Re: lua error when trying to destroy a corpse entity

Posted: Fri Jul 08, 2016 8:15 pm
by DedlySpyder
Not sure exactly, check if the entity is valid first and by itself (nest everything inside that if statement)

Re: lua error when trying to destroy a corpse entity

Posted: Fri Jul 08, 2016 8:17 pm
by prg

Code: Select all

    for _, entity in pairs(game.surfaces['nauvis'].find_entities{area_start, area_end}) do
      if entity.type == 'fire' then
        entity.destroy()
      end

      if entity.type == 'transport-belt' then
        event.entity.destroy() -- THIS CAUSE THE CRASH
      end
    end
Things that seem like issues on first glance:
  • You hardcode game.surfaces['nauvis'] when you could just be using event.entity.surface (probably not the problem here, but still.)
  • If you find a fire you destroy the entity, then check that same entity for type == 'transport-belt'. Use elseif here.
  • If you find multiple transport-belts, you destroy event.entity multiple times. The .valid check should help here.
If you still have problems provide the complete code so other people can actually test it instead of making wild guesses.

Re: lua error when trying to destroy a corpse entity

Posted: Fri Jul 08, 2016 9:04 pm
by devilwarriors
DedlySpyder wrote:check if the entity is valid first and by itself (nest everything inside that if statement)
this solved my problem, thanks!