# Creating widgets inside a transaction (and calling Undo)

**URL:** <https://forums.unrealengine.com/t/creating-widgets-inside-a-transaction-and-calling-undo/1213497>\
**Category:** Editor Scripting\
**Tags:** unreal-engine, editor, Widget, question, CPP, UMG\
**Created:** [June 28, 2023, 3:37pm UTC](https://forums.unrealengine.com/t/creating-widgets-inside-a-transaction-and-calling-undo/1213497 "2023-06-28T15:37:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcelSchulte](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MarcelSchulte](https://forums.unrealengine.com/u/MarcelSchulte)\
**Post date:** [June 28, 2023, 3:37pm UTC](https://forums.unrealengine.com/t/creating-widgets-inside-a-transaction-and-calling-undo/1213497/1 "2023-06-28T15:37:37Z")

</div>

Hello, I am fairly new to editor scripting in unreal and I want to implement undo/redo functionality for my EditorUtilityWidget.

Inside my widget i frequently spawn custom UserWidgets, for example as editable nodes.  
I want to track everything the user can do with transactions.  
But when i spawn widgets and call undo, the widgets references all disappear, but they are still displayed in the editor window, with all interaction gone.

One of my Spawns looks like this:

```auto
UMissionEditor_MissionNode* UMissionEditor_Window::SpawnNode_Internal(
	const TSubclassOf<UMissionEditor_MissionNode> WidgetClass)
{
	if(!Mission || !WidgetClass || !NodesCanvas)
		return nullptr;

	FScopedTransaction Transaction(FText::FromString("Spawn Editor Node"));
	
	UMissionEditor_MissionNode* NewWidget = WidgetTree->ConstructWidget<UMissionEditor_MissionNode>(WidgetClass);
	NewWidget->Modify();
	NodesCanvas->Modify();

	NodesCanvas->AddChild(NewWidget);
	Cast<UCanvasPanelSlot>(NewWidget->Slot)->SetPosition(LocalMousePos - Cast<UCanvasPanelSlot>(NodesCanvas->Slot)->GetPosition());

	NewWidget->OnMouseEvent.BindDynamic(this, &UMissionEditor_Window::UMissionEditor_Window::OnMouseMoveEvent);
	NewWidget->SetID(Mission->IDCounter++);

	return NewWidget;
}

```

I have tried with and without the Modify and everything else i could find about transactions.

Is there something i am missing or do transactions just not handle widget spawning well?

---

<div class="post-metadata">

**Author:** ![MarcelSchulte](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MarcelSchulte](https://forums.unrealengine.com/u/MarcelSchulte)\
**Post date:** [June 29, 2023, 2:04pm UTC](https://forums.unrealengine.com/t/creating-widgets-inside-a-transaction-and-calling-undo/1213497/2 "2023-06-29T14:04:30Z")

</div>

So for now i ended creating my own Undo and Redo implementations:

I Created a class that derives from FEditorUndoClient for every Type of Transaction i want to custom implement. That class has the following Method (“Editor” is a pointer to my EditorUtilityWidget):

```auto
void FTransactionBase::RegisterTransaction(const FString& Name)
{
	GEditor->BeginTransaction(*GetTransactionContext(),FText::FromString(Name), Editor->TransactionDummy.Get());
	Editor->TransactionDummy->Modify();
	Editor->TransactionDummy->DummyBool = !Editor->TransactionDummy->DummyBool;
	GEditor->EndTransaction();
}

```

this just registers an empty undo event to that i can react inside my class.

The TransactionDummy is a Dummy UObject, that has a bool UPROPERTY. Its only purpose is to register some form ob change.

Then i implemented my own logic in the derived PostUndo and PostRedo.

I am still curious about making this work with the integrated Transaction logic, so if anybody has any insight, please leave a comment.
