# Get a reference to component owner while in blueprint editor

**URL:** <https://forums.unrealengine.com/t/get-a-reference-to-component-owner-while-in-blueprint-editor/568421>\
**Category:** C++\
**Tags:** actor-blueprint, unreal-engine, UE5-0, Components, question, Blueprint\
**Created:** [May 28, 2022, 5:28am UTC](https://forums.unrealengine.com/t/get-a-reference-to-component-owner-while-in-blueprint-editor/568421 "2022-05-28T05:28:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruno\_tvs](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bruno\_tvs](https://forums.unrealengine.com/u/Bruno_tvs)\
**Post date:** [May 28, 2022, 5:28am UTC](https://forums.unrealengine.com/t/get-a-reference-to-component-owner-while-in-blueprint-editor/568421/1 "2022-05-28T05:28:17Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![SeducerJorge](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SeducerJorge](https://forums.unrealengine.com/u/SeducerJorge)\
**Post date:** [May 28, 2022, 3:12pm UTC](https://forums.unrealengine.com/t/get-a-reference-to-component-owner-while-in-blueprint-editor/568421/2 "2022-05-28T15:12:16Z")

</div>

Try using a static class for your MyFunction so like

```auto
else
{
 MyFunction(AOwnerClass::StaticClass)
}

```

---

<div class="post-metadata">

**Author:** ![Bruno\_tvs](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bruno\_tvs](https://forums.unrealengine.com/u/Bruno_tvs)\
**Post date:** [May 28, 2022, 4:25pm UTC](https://forums.unrealengine.com/t/get-a-reference-to-component-owner-while-in-blueprint-editor/568421/3 "2022-05-28T16:25:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JornamGregor](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JornamGregor](https://forums.unrealengine.com/u/JornamGregor)\
**Post date:** [October 3, 2022, 2:23pm UTC](https://forums.unrealengine.com/t/get-a-reference-to-component-owner-while-in-blueprint-editor/568421/4 "2022-10-03T14:23:45Z")

</div>

For anyone Googling this, I found my solution here: [Actor.GetComponents() returns Null for blueprint generated actor - #8 by M.Haenssgen](https://forums.unrealengine.com/t/actor-getcomponents-returns-null-for-blueprint-generated-actor/452964/8)

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).

```auto
	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;

```

---

<div class="post-metadata">

**Author:** ![Mind-Brain](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mind-brain/32/906573_2.png) [@Mind-Brain](https://forums.unrealengine.com/u/Mind-Brain)\
**Post date:** [October 5, 2022, 5:54pm UTC](https://forums.unrealengine.com/t/get-a-reference-to-component-owner-while-in-blueprint-editor/568421/5 "2022-10-05T17:54:41Z")

</div>

JornamGregor coming in clutch!

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

---

<div class="post-metadata">

**Author:** ![Ciberus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ciberus/32/561261_2.png) [@Ciberus](https://forums.unrealengine.com/u/Ciberus)\
**Post date:** [July 8, 2023, 1:26pm UTC](https://forums.unrealengine.com/t/get-a-reference-to-component-owner-while-in-blueprint-editor/568421/6 "2023-07-08T13:26:38Z")

</div>

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()`

> [@How to get a component from a ClassDefaultObject?](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/9):
>
> Found solution on how to get all ActorComponents(both created in C++ and Blueprint) from another ActorComponent(non SceneComponent). If you need to get ActorComponents from Actor itself replace GetOuter()-\>GetClass() on GetOwner()-\>GetClass() code example static TArray\<FString\> GetHitRegistratorsNames(UObject\* OwnerObject) { TArray\<FString\> Result = {}; UBlueprintGeneratedClass\* BlueprintGeneratedClass = Cast\<UBlueprintGeneratedClass\>(OwnerObject); …

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [August 7, 2023, 1:27pm UTC](https://forums.unrealengine.com/t/get-a-reference-to-component-owner-while-in-blueprint-editor/568421/7 "2023-08-07T13:27:28Z")

</div>

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