Implementing undo and redo actions in Unreal Engine
If you find yourself implementing a tool in Unreal that works in editor time and does modify some assets or instantiate new one on a level you for sure will run into a need of using undo and redo actions. Luckily Unreal does make it quite easy to implement these. There really are only four functions that we are interested in:
UKismetSystemLibrary::BeginTransaction
UKismetSystemLibrary::EndTransaction
UKismetSystemLibrary::CancelTransaction
UKismetSystemLibrary::TransactObject
You need to enclose all your asset modifications within BeginTransaction
and EndTransaction
call. If you are spawning new actors, you don’t need to do anything about them. However if you are just modifying properties of an already existing asset then you need to call TransactObject
function which accepts UObject
class instance. If for any reason you would like to cancel the transaction just call CancelTransaction
.
Here is an example usage in action:
UKismetSystemLibrary::BeginTransaction("Context", FText::FromString("Action description"), nullptr);
UKismetSystemLibrary::TransactObject(ExitingActorToModify);
ExistingActorToModify->SetActorLocation(FVector(100.0f));
for (int32 Index = 0; Index < 10; Index++)
{
GetWorld()->SpawnActor<AActor>();
}
UKismetSystemLibrary::EndTransaction();