Get a reference to component owner while in blueprint editor

Is it possible to get a reference to a actor component owner class, so I can harvest a class variable, before spawning that owner?

It would be something like:

UMyActorComponent::MyActorComponet()
{
    AActor* Owner = GetOwner();
    if(IsValid(Owner))
    {
        UClass* OwnerClass = Owner->GetClass();

        MyFunction(OwnerClass);
    }
}

The problem with that snippet is that the owner is a nullptr for some calls. So, while in blueprint editor, “MyFunction()” would not be called. What I want is some way to call MyFunction that needs a reference to the owner actor. I dont know if this is directly possible through some UActorComponent event, like “OnComponentCreated()” or if I have to set a specific creation method, or use SCS. I have spent some time with this problem without any succes, the best I was able to get was to run “MyFunction” when the owning actor gets spawned on the world.

Try using a static class for your MyFunction so like

else
{
 MyFunction(AOwnerClass::StaticClass)
}

Thanks for the answer Jorge.

The problem is I want to get a pointer to every scene component added to a blueprint class and show it while in the blueprint editor.

For anyone Googling this, I found my solution here: Actor.GetComponents() returns Null for blueprint generated actor - #8 by M.Haenssgen

In the editor, the component has no owner, but you can use the the blueprint generated class outer, which leads you to the “nodes” (aka components).

	TArray<FString> options;

	UBlueprintGeneratedClass* owningGenClass = Cast<UBlueprintGeneratedClass>(GetOuter());
	if (owningGenClass == nullptr)
	{
		return options;
	}
	const TObjectPtr<USimpleConstructionScript> constructionScript = owningGenClass->SimpleConstructionScript;
	if (constructionScript == nullptr)
	{
		return options;
	}
	for (const USCS_Node* component : constructionScript->GetAllNodes())
	{
		const UMeshComponent* meshComponent = Cast<UMeshComponent>(component->ComponentTemplate);
		if (meshComponent != nullptr)
		{
			FName name = component->GetVariableName();
			if ( ! meshComponents.Contains(name))
			{
				options.Add(name.ToString());
			}
		}
	}
	return options;
2 Likes

JornamGregor coming in clutch!

@Bruno_tvs Did that solve your problem? Let us know!

Thanks a lot, i have small addition to your solution.

GetAllNodes() returns only ActorComponents created in Blueprint here how to get ActorComponents created in C++ also, via BlueprintGeneratedClass->GetDefaultObjectSubobjects()

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