What I've found is that removing a 'removed-entity' action will also remove the paired 'built-entity' action.
I don't know if this needs to be within the built event, or even the same tick. I also haven't checked if the inverse will work either (but you don't tend to have access to the 'built-entity' during a built event)
I think knowing about this I would've gotten far less upset about how we don't have access to the source undo/redo item during a built event.
Maybe an example code of updating the undo/redo stack when swapping out a placed entity will help people understand?
Code: Select all
-- on_built_event
surface.create_entity{
name = "replaced-entity",
position = built_entity.last_user,
player = player,
fast_replace = true
}
-- Get the stack and the undo/redo item just created or added to
local undo_stack = player.undo_redo_stack
local undo_actions = undo_stack.get_undo_item(1)
-- Remove the second to last action, the 'removed-entity' action, just created
-- Will also remove the original 'built-entity' action
undo_stack.remove_undo_action(1, #undo_actions-1)