Skip to content

[0.7.56] can not listen to creation events for space elevators #393

@hgschmie

Description

@hgschmie

I have a mod that has an interest to know when a space elevator is created. So I set up code that listens to the creation events (defines.events.on_built_entity, defines.events.on_robot_built_entity, defines.events.on_space_platform_built_entity, defines.events.script_raised_built, defines.events.script_raised_revive) for se-space-elevator entities.

Now, my mod does not receive these events all the time. To be exact, the event is only received in two of the four possible orientations of a space elevator (as a assembling machine, it has four possible orientations and you can rotate through those).

This was surprising until I realized that the current code does this during the "on_entity_created" handler:

if direction ~= entity.direction then
    local old_entity = entity
    entity = surface.create_entity{
      name = SpaceElevator.name_space_elevator,
      position = position,
      direction = direction,
      force = force
    }
    ---@cast entity -?
    old_entity.destroy()
end

As the created entity is discarded within the handler, Factorio does not call other handlers (the entity that it would call about has been invalidated).

Now, this would be a minor annoyance if the create_entity call would have raise_built = true. In that case, my mod would receive that event for the new entity that was created within that handler.

This fixes the problem:

diff --git i/scripts/space-elevator.lua w/scripts/space-elevator.lua
index 93de605..4d9f8cf 100644
--- i/scripts/space-elevator.lua
+++ w/scripts/space-elevator.lua
@@ -464,10 +464,12 @@ function SpaceElevator.on_entity_created(event)
       name = SpaceElevator.name_space_elevator,
       position = position,
       direction = direction,
+      raise_built = true,
       force = force
     }
     ---@cast entity -?
     old_entity.destroy()
+    return
   end

   -- returns nil on failed create

Note that you MUST add the return, otherwise you win https://forums.factorio.com/viewtopic.php?t=133665

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions