Set variable in component when actor is placed in editor

Hello everyone,

I am trying to set a variable inside a component whenever an actor with that component is placed in a world in the editor. The use case for this is to create a GUID for the component when it is placed in the editor. How can something like this be accomplished? Thanks. :slight_smile:

I ended up writing an editor module that assigns a GUID when a new actor is dragged into the scene like this:

void FEditorModule::StartupModule()
{
	ActorDragged = FEditorDelegates::OnNewActorsDropped.AddRaw(this, &FEditorModule::OnActorDragged);
}

void FEditorModule::ShutdownModule()
{
	FEditorDelegates::OnNewActorsDropped.Remove(ActorDragged);
}

void FEditorModule::OnActorDragged(const TArray <UObject*>& Objects, const TArray<AActor*>& Actors)
{
	// assign a GUID to the guid component of any actor dragged into the world
	for (AActor* Actor : Actors)
	{
		UGuidComponent* GuidComponent = Cast<UGuidComponent>(Actor->GetComponentByClass(UGuidComponent::StaticClass()));
		if (GuidComponent)
		{
			GuidComponent->SetupComponent();
		}
	}
}

This only works if you add the new actor via Drag & Drop. Is there a method that also works when using duplicate actor or right click place actor?

You can use PostEditImport.

See here for more information:
https://forums.unrealengine.com/unreal-engine/marketplace/89470-binary-compressed-save-system-save-entire-dynamically-generated-worlds-%E2%99%A5-rama?p=885846#post885846