Event for Deconstruction an Entity

Place to get help with not working mods / modding interface.
Rumo
Burner Inserter
Burner Inserter
Posts: 11
Joined: Fri Sep 09, 2022 9:27 pm
Contact:

Event for Deconstruction an Entity

Post by Rumo »

Hello,
is there any Event triggering when i deconstruct/remove an entity?

Thanks for your help.
User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 468
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

Re: Event for Deconstruction an Entity

Post by Stringweasel »

Yes, there are. It would help reading the docs :) https://lua-api.factorio.com/latest/def ... nes.events

For example you typically listen to these events

Code: Select all

script.on_event(defines.events.on_player_mined_entity, your_function)
script.on_event(defines.events.on_robot_mined_entity, your_function)
script.on_event(defines.events.on_entity_died, your_function)
script.on_event(defines.events.script_raised_destroy, your_function)
Alt-F4 Author | Factorio Modder
Probably known for: (Configurable) Valves | Better Victory Screen | Space Spidertron | Fluidic Power
Official Contributor to Space Exploration
Rumo
Burner Inserter
Burner Inserter
Posts: 11
Joined: Fri Sep 09, 2022 9:27 pm
Contact:

Re: Event for Deconstruction an Entity

Post by Rumo »

Hey,
thanks for the help.

I read the docs and tried some trigger but nothing happens :)

But then i will try it again.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1758
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Event for Deconstruction an Entity

Post by Pi-C »

Rumo wrote: Sun Sep 11, 2022 2:28 pm I read the docs and tried some trigger but nothing happens :)
Are you sure that the event handler is really active? Add this to your control.lua:

Code: Select all

  local c_name = "show-active-events"
  if not commands.commands[c_name] then
    commands.add_command(c_name, c_name, function(command)

      local player = game.players[command.player_index]
      local events = {}
      for event_name, event in pairs(defines.events) do
        if script.get_event_handler(event) then
          events[event] = event_name
        end
      end
      local msg = serpent.block(events)
      player.print(msg)
      log(msg)

    end
  else
    error("Command \""..c_name.."\" already exists, change c_name!")
  end

It will add a command that you can run from the chat console with {code]/show-active-events[/code] to get the list of events your mod is listening to.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 468
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

Re: Event for Deconstruction an Entity

Post by Stringweasel »

Or it could be that you code isn't running :)

Does Factorio recognise your mod? Are you placing your code in `control.lua`? How are you testing if it's running or not?
Alt-F4 Author | Factorio Modder
Probably known for: (Configurable) Valves | Better Victory Screen | Space Spidertron | Fluidic Power
Official Contributor to Space Exploration
Post Reply

Return to “Modding help”