Table of Contents

Class UndoRedo

Namespace
Godot
Assembly
GodotSharp.dll

UndoRedo works by registering methods and property changes inside "actions". You can create an action, then provide ways to do and undo this action using function calls and property changes, then commit the action.

When an action is committed, all of the do_* methods will run. If the Undo() method is used, the undo_* methods will run. If the Redo() method is used, once again, all of the do_* methods will run.

Here's an example on how to add an action:

private UndoRedo _undoRedo;

public override void _Ready() { _undoRedo = new UndoRedo(); }

public void DoSomething() { // Put your code here. }

public void UndoSomething() { // Put here the code that reverts what's done by "DoSomething()". }

private void OnMyButtonPressed() { var node = GetNode<Node2D>("MyNode2D"); _undoRedo.CreateAction("Move the node"); _undoRedo.AddDoMethod(new Callable(this, MethodName.DoSomething)); _undoRedo.AddUndoMethod(new Callable(this, MethodName.UndoSomething)); _undoRedo.AddDoProperty(node, "position", new Vector2(100, 100)); _undoRedo.AddUndoProperty(node, "position", node.Position); _undoRedo.CommitAction(); }

Before calling any of the add_(un)do_* methods, you need to first call CreateAction(string, MergeMode, bool). Afterwards you need to call CommitAction(bool).

If you don't need to register a method, you can leave AddDoMethod(Callable) and AddUndoMethod(Callable) out; the same goes for properties. You can also register more than one method/property.

If you are making an EditorPlugin and want to integrate into the editor's undo history, use EditorUndoRedoManager instead.

If you are registering multiple properties/method which depend on one another, be aware that by default undo operation are called in the same order they have been added. Therefore instead of grouping do operation with their undo operations it is better to group do on one side and undo on the other as shown below.

_undo_redo.CreateAction("Add object");

// DO _undo_redo.AddDoMethod(new Callable(this, MethodName.CreateObject)); _undo_redo.AddDoMethod(new Callable(this, MethodName.AddObjectToSingleton));

// UNDO _undo_redo.AddUndoMethod(new Callable(this, MethodName.RemoveObjectFromSingleton)); _undo_redo.AddUndoMethod(new Callable(this, MethodName.DestroyThatObject));

_undo_redo.CommitAction();

public class UndoRedo : GodotObject, IDisposable
Inheritance
UndoRedo
Implements
Inherited Members

Constructors

UndoRedo()

public UndoRedo()

Methods

AddDoMethod(Callable)

Register a Callable that will be called when the action is committed.

public void AddDoMethod(Callable callable)

Parameters

callable Callable

AddDoProperty(GodotObject, StringName, Variant)

Register a property that would change its value to value when the action is committed.

public void AddDoProperty(GodotObject @object, StringName property, Variant value)

Parameters

object GodotObject
property StringName
value Variant

AddDoReference(GodotObject)

Register a reference for "do" that will be erased if the "do" history is lost. This is useful mostly for new nodes created for the "do" call. Do not use for resources.

var node = Node2D.new()
  undo_redo.create_action("Add node")
  undo_redo.add_do_method(add_child.bind(node))
  undo_redo.add_do_reference(node)
  undo_redo.add_undo_method(remove_child.bind(node))
  undo_redo.commit_action()
public void AddDoReference(GodotObject @object)

Parameters

object GodotObject

AddUndoMethod(Callable)

Register a Callable that will be called when the action is undone.

public void AddUndoMethod(Callable callable)

Parameters

callable Callable

AddUndoProperty(GodotObject, StringName, Variant)

Register a property that would change its value to value when the action is undone.

public void AddUndoProperty(GodotObject @object, StringName property, Variant value)

Parameters

object GodotObject
property StringName
value Variant

AddUndoReference(GodotObject)

Register a reference for "undo" that will be erased if the "undo" history is lost. This is useful mostly for nodes removed with the "do" call (not the "undo" call!).

var node = $Node2D
  undo_redo.create_action("Remove node")
  undo_redo.add_do_method(remove_child.bind(node))
  undo_redo.add_undo_method(add_child.bind(node))
  undo_redo.add_undo_reference(node)
  undo_redo.commit_action()
public void AddUndoReference(GodotObject @object)

Parameters

object GodotObject

ClearHistory(bool)

Clear the undo/redo history and associated references.

Passing false to increaseVersion will prevent the version number from increasing when the history is cleared.

public void ClearHistory(bool increaseVersion = true)

Parameters

increaseVersion bool

CommitAction(bool)

Commit the action. If execute is true (which it is by default), all "do" methods/properties are called/set when this function is called.

public void CommitAction(bool execute = true)

Parameters

execute bool

CreateAction(string, MergeMode, bool)

Create a new action. After this is called, do all your calls to AddDoMethod(Callable), AddUndoMethod(Callable), AddDoProperty(GodotObject, StringName, Variant), and AddUndoProperty(GodotObject, StringName, Variant), then commit the action with CommitAction(bool).

The way actions are merged is dictated by mergeMode. See UndoRedo.MergeMode for details.

The way undo operation are ordered in actions is dictated by backwardUndoOps. When backwardUndoOps is false undo option are ordered in the same order they were added. Which means the first operation to be added will be the first to be undone.

public void CreateAction(string name, UndoRedo.MergeMode mergeMode = MergeMode.Disable, bool backwardUndoOps = false)

Parameters

name string
mergeMode UndoRedo.MergeMode
backwardUndoOps bool

EndForceKeepInMergeEnds()

Stops marking operations as to be processed even if the action gets merged with another in the Ends mode. See StartForceKeepInMergeEnds().

public void EndForceKeepInMergeEnds()

GetActionName(int)

Gets the action name from its index.

public string GetActionName(int id)

Parameters

id int

Returns

string

GetCurrentAction()

Gets the index of the current action.

public int GetCurrentAction()

Returns

int

GetCurrentActionName()

Gets the name of the current action, equivalent to get_action_name(get_current_action()).

public string GetCurrentActionName()

Returns

string

GetHistoryCount()

Returns how many elements are in the history.

public int GetHistoryCount()

Returns

int

GetVersion()

Gets the version. Every time a new action is committed, the UndoRedo's version number is increased automatically.

This is useful mostly to check if something changed from a saved version.

public ulong GetVersion()

Returns

ulong

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

HasRedo()

Returns true if a "redo" action is available.

public bool HasRedo()

Returns

bool

HasUndo()

Returns true if an "undo" action is available.

public bool HasUndo()

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsCommittingAction()

Returns true if the UndoRedo is currently committing the action, i.e. running its "do" method or property change (see CommitAction(bool)).

public bool IsCommittingAction()

Returns

bool

Redo()

Redo the last action.

public bool Redo()

Returns

bool

StartForceKeepInMergeEnds()

Marks the next "do" and "undo" operations to be processed even if the action gets merged with another in the Ends mode. Return to normal operation using EndForceKeepInMergeEnds().

public void StartForceKeepInMergeEnds()

Undo()

Undo the last action.

public bool Undo()

Returns

bool

Events

VersionChanged

Called when Undo() or Redo() was called.

public event Action VersionChanged

Event Type

Action