Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

EditorUndoRedoManager

繼承: Object

管理編輯器中打開場景的撤銷歷史。

說明

EditorUndoRedoManager 是將 UndoRedo 物件與編輯場景相關聯的管理器。每個場景都有自己的撤銷歷史,EditorUndoRedoManager 能夠確保編輯器中執行的每個動作都與正確的場景相關聯。與場景無關的動作(對 ProjectSettings、外部資源等物件的編輯)使用單獨的全域歷史。

用法與 UndoRedo 基本一致。需要建立並提交動作,然後管理器會自動決定這個動作屬於哪個場景。場景是根據該動作中第一個操作所使用的物件來推斷的。規則如下:

  • 如果該物件為 Node,則使用目前編輯的場景;

  • 如果該物件為內建資源,則使用其路徑上的場景;

  • 如果該物件為外部資源或任何其他物件,則使用全域歷史。

推斷的結果有時並不準確,所以在建立動作時你可以提供自訂的本文物件。

EditorUndoRedoManager 是為 Godot 編輯器外掛程式使用而設計的。你可以使用 EditorPlugin.get_undo_redo() 獲取。對於非編輯器使用場景或者不需要與編輯器撤銷歷史記錄集成的外掛程式,請改用 UndoRedo

管理器的 API 與 UndoRedo 基本一致,它的文件中有更多範例。主要區別在於 EditorUndoRedoManager 的動作使用物件 + 方法名,而不是 Callable

方法

void

add_do_method(object: Object, method: StringName, ...) vararg

void

add_do_property(object: Object, property: StringName, value: Variant)

void

add_do_reference(object: Object)

void

add_undo_method(object: Object, method: StringName, ...) vararg

void

add_undo_property(object: Object, property: StringName, value: Variant)

void

add_undo_reference(object: Object)

void

clear_history(id: int = -99, increase_version: bool = true)

void

commit_action(execute: bool = true)

void

create_action(name: String, merge_mode: MergeMode = 0, custom_context: Object = null, backward_undo_ops: bool = false, mark_unsaved: bool = true)

void

force_fixed_history()

UndoRedo

get_history_undo_redo(id: int) const

int

get_object_history_id(object: Object) const

bool

is_committing_action() const


訊號

history_changed() 🔗

當任何歷史中的動作列表發生變化時發出,無論是當一個動作被提交或一個歷史被清除時。


version_changed() 🔗

當任何歷史記錄的版本因撤銷或重做呼叫而變化時發出。


列舉

enum SpecialHistory: 🔗

SpecialHistory GLOBAL_HISTORY = 0

全域歷史不與任何場景相關聯,但與外部資源等相關聯。

SpecialHistory REMOTE_HISTORY = -9

與遠程屬性檢視器相關的歷史。在即時編輯正在運作的遊戲專案時使用。

SpecialHistory INVALID_HISTORY = -99

無效歷史“null”。這是一個特殊值,不與任何物件相關聯。


方法說明

void add_do_method(object: Object, method: StringName, ...) vararg 🔗

註冊一個方法,當動作被提交(即“做”的動作)時將被呼叫。

如果這是第一次操作,object 將被用於推斷目標撤銷歷史。


void add_do_property(object: Object, property: StringName, value: Variant) 🔗

為“做”註冊一個屬性值變更。

如果這是第一次操作,object 將被用於推斷目標撤銷歷史。


void add_do_reference(object: Object) 🔗

為“做”註冊一個引用,如果“做”歷史丟失,則該引用將被擦除。這主要用於為“做”呼叫而建立的新節點。請不要用於資源。


void add_undo_method(object: Object, method: StringName, ...) vararg 🔗

註冊一個方法,當動作被撤銷時(即“撤銷”動作)將被呼叫。

如果這是第一次操作,object 將被用於推斷目標撤銷歷史。


void add_undo_property(object: Object, property: StringName, value: Variant) 🔗

為“撤銷”註冊一個屬性值變更。

如果這是第一次操作,object 將被用於推斷目標撤銷歷史。


void add_undo_reference(object: Object) 🔗

為“撤銷”註冊一個引用,如果“撤銷”歷史丟失,則該引用將被擦除。這主要用於通過“做”呼叫(而不是“撤銷”呼叫!)而移除的節點。


void clear_history(id: int = -99, increase_version: bool = true) 🔗

Clears the given undo history. You can clear history for a specific scene, global history, or for all histories at once (except REMOTE_HISTORY) if id is INVALID_HISTORY.

If increase_version is true, the undo history version will be increased, marking it as unsaved. Useful for operations that modify the scene, but don't support undo.

var scene_root = EditorInterface.get_edited_scene_root()
var undo_redo = EditorInterface.get_editor_undo_redo()
undo_redo.clear_history(undo_redo.get_object_history_id(scene_root))

Note: If you want to mark an edited scene as unsaved without clearing its history, use EditorInterface.mark_scene_as_unsaved() instead.


void commit_action(execute: bool = true) 🔗

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


void create_action(name: String, merge_mode: MergeMode = 0, custom_context: Object = null, backward_undo_ops: bool = false, mark_unsaved: bool = true) 🔗

Create a new action. After this is called, do all your calls to add_do_method(), add_undo_method(), add_do_property(), and add_undo_property(), then commit the action with commit_action().

The way actions are merged is dictated by the merge_mode argument.

If custom_context object is provided, it will be used for deducing target history (instead of using the first operation).

The way undo operation are ordered in actions is dictated by backward_undo_ops. When backward_undo_ops 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.

If mark_unsaved is false, the action will not mark the history as unsaved. This is useful for example for actions that change a selection, or a setting that will be saved automatically. Otherwise, this should be left to true if the action requires saving by the user or if it can cause data loss when left unsaved.


void force_fixed_history() 🔗

Forces the next operation (e.g. add_do_method()) to use the action's history rather than guessing it from the object. This is sometimes needed when a history can't be correctly determined, like for a nested resource that doesn't have a path yet.

This method should only be used when absolutely necessary, otherwise it might cause invalid history state. For most of complex cases, the custom_context parameter of create_action() is sufficient.


UndoRedo get_history_undo_redo(id: int) const 🔗

返回與給定歷史 id 關聯的 UndoRedo 對象。

0 以上的 id 被對應到打開的場景分頁(但它與它們的順序不配對)。0 或更低的 id 具有特殊含義(參閱 SpecialHistory)。

最好與 get_object_history_id() 一起使用。該方法被提供,只是以防需要 UndoRedo 的一些更高級的方法的情況(但請記住,直接操作 UndoRedo 物件可能會影響編輯器的穩定性)。


int get_object_history_id(object: Object) const 🔗

返回從給定的 object 推匯出的歷史 ID。它可以與 get_history_undo_redo() 一起使用。


bool is_committing_action() const 🔗

如果 EditorUndoRedoManager 目前正在提交該動作,即運作其“做”方法或屬性更改(請參閱 commit_action())時,則返回 true