Binding to editor undo/redo in an Editor Utility Widget

Sometimes you're using an Editor Utility Widget to present data about something in the level, and you update this info when the data changes by whatever means, but when it changes via Undo/Redo, maybe you don't have an easy way to bind to that. You'd think this would be exposed to BP already for editor utilities, but it isn't. Here's how I just did it:

  1. Make a CPP subclass of EditorUtilityWidget (not EditorUtilityWidgetBlueprint)
  2. In your .h, implement the interface FEditorUndoClient
  3. Override NativeConstruct() and make a BlueprintImplementableEvent called something like OnPostUndo
  4. In NativeConstruct(), bind your event to FEditorDelegates::PostUndoRedo
  5. Implement your event in Blueprint and do your refreshing/whatever you need to do.

My .h:

UCLASS()
class WITCHEDITOR_API USOHEditorUtilityWidget : public UEditorUtilityWidget, public FEditorUndoClient
{
	GENERATED_BODY()
public:
	USOHEditorUtilityWidget();
	UFUNCTION(BlueprintImplementableEvent)
	void OnPostUndo();

protected:
	virtual void NativeConstruct() override;
};

My .cpp:

{
	Super::NativeConstruct();
	// When undo occurs, get a notification so we can make sure our view is up to date
	FEditorDelegates::PostUndoRedo.AddUObject(this, &USOHEditorUtilityWidget::OnPostUndo);
}

Another method that did not work for me but would be the go in other circumstances would be to use GEditor->RegisterForUndo(this); and override PostUndo and PostRedo, but this did not work for me; I'm guessing it only gives you back an event if the object you register is involved in the transaction in question, whereas what I wanted for this is any undo or redo at all.

1 Like