Is there a better way to get the Global UndoRedo in a Tool Script?

Godot Version

4.5.1

Question

I am working with a tool script which extends Resource, so the UndoRedo shouldn’t be tied to the current scene. I have gotten a workaround solution to work, which is getting a dummy EditorPlugin’s UndoRedo and freeing the dummy immediately:

var dummy_ep := EditorPlugin.new()
var undo_redo := dummy_ep.get_undo_redo()
dummy_ep.free()

I wanted to ask, is there a more straight forward way to gain access to the global UndoRedo?

I don’t think there is.

If there really is no way in GDscript to fetch the global UndoRedo then I’ll probably draft a proposal to the github and see if anyone sees it as a worthwhile enhancement.

You can get it with EditorInterface.get_editor_undo_redo()

EditorInterface is only available in the editor and won’t work at runtime. If the script needs to work at runtime you can get it using Engine.get_singleton()

For example:

extends Resource


func do_something() -> void:
    if Engine.is_editor_hint():
        var editor_interface = Engine.get_singleton("EditorInterface")
        var undo_redo = editor_interface.get_editor_undo_redo()
        # ...
2 Likes

That’s exactly what I was looking for, thank you!