UndoRedoManager add and remove nodes for plugin

Godot Version

4.4

Question

What is the canonical way to implement undo and redo for a newly created node in a scene?
Can I find an example of a proper implementation somewhere?

Details

I can’t figure out what to do with the UndoRedoManager in case of adding and removing nodes.

I’m convinced it requires this method somehow: add_undo_reference – but for the life of me, I cannot parse what the documentation is trying to tell me here.

I’ve tried using queue_free / free on the node in the undo-method, but redo fails this way.

See also

I’m not sure but here’s how the scene dock does it when adding a node godot/editor/scene_tree_dock.cpp at be3ecaeb3c51433058ad4e96ec892d18f4291efd · godotengine/godot · GitHub

1 Like

Thank you, I think this will work. I will verify it as soon as I’m able.

I think my mistake was to call free() on the node, in stead of only removing it from the tree, like this subsnippet does:

Guess I’ll also need to check if I need to replicate the _post_do_create method as well:

Just wondering, will the reference to this orphaned node then be cleaned up by the undo_redo_manager later?

That’s what the add_do_reference() does when erasing the history as far as I can tell from the code.

This add it to a list of references to delete:

And here deletes them when the action can’t be re-done:

1 Like

Youre right!

I’m sorry, I must be turning blind.. it’s right here in the snippet your shared:

Thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.