How to detect the Transform change of the Component's Owner in the Editor ?

Hello,

I’m trying to find a solution to the similar problem as:

I have a Component which I use for custom movement and I want to snap its Owner to the specific World Location whenever said Owner is being moved in the Level Viewport.

This seems to be pretty easy to do for Actors by overriding AActor::PostEditMove

but what if I want to detect it inside of Component?
I could of course access the Owning Actor via UActorComponent::GetOwner but what then? I cannot just override some of the Actor’s functions inside of actual Component (sadly).

Are there any other overridable functions in UActorComponent or USceneComponent that I could use to achieve the similar behavior? Or any delegates that I could bind to?

Found the solution!

When AActor moves, what actually moves is its Root Component.

There is a delegate USceneComponent::TransformUpdated and Actor always has the Scene Component at its root so I can easily call GetOwner()->GetRootComponent() and bind something to said delegate. I’m doing this inside of UActorComponent::OnComponentCreated override.

Full code:

.h

	/* Note: This is inside of class derived from UActorComponent */
	virtual void OnComponentCreated() override;

.cpp

void UMyComponent::OnComponentCreated()
{
	Super::OnComponentCreated();

	const UWorld* const ThisWorld = GetWorld();
	if (
		this->HasAnyFlags(RF_Transient)
		&& !IsValid(ThisWorld)
		&& !ThisWorld->IsEditorWorld()
		&& ThisWorld->IsPlayInEditor()
		)
	{
		return;
	}
	
	auto OnOwnerTransformUpdated =
		[this](USceneComponent* UpdatedComponent, EUpdateTransformFlags UpdateTransformFlag, ETeleportType Teleport)->void
		{
			/* Do stuff with transform here! */
		};

	GetOwner()->GetRootComponent()->TransformUpdated.AddLambda(OnOwnerTransformUpdated);
}

Result:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.