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

Place to get help with not working mods / modding interface.
Post Reply
devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

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

Post 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!
Last edited by devilwarriors on Fri Jul 08, 2016 9:05 pm, edited 1 time in total.

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 253
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: lua error when trying to destroy a corpse entity

Post by DedlySpyder »

You need to check the entity.valid to see if you can affect it.

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: lua error when trying to destroy a corpse entity

Post 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

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: lua error when trying to destroy a corpse entity

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

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 253
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: lua error when trying to destroy a corpse entity

Post by DedlySpyder »

Not sure exactly, check if the entity is valid first and by itself (nest everything inside that if statement)

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: lua error when trying to destroy a corpse entity

Post 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.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: lua error when trying to destroy a corpse entity

Post 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!

Post Reply

Return to “Modding help”