# Editable Sub-Object Component Pointers

**URL:** <https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612>\
**Category:** UI\
**Tags:** uobject, unreal-engine, editor, Components, question, UMG\
**Created:** [April 10, 2015, 9:46pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612 "2015-04-10T21:46:07Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_c788a779](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c788a779](https://forums.unrealengine.com/u/anonymous_user_c788a779)\
**Post date:** [April 10, 2015, 9:46pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/1 "2015-04-10T21:46:07Z")

</div>

This is sort of a broad question concerning how sub-object pointers are meant to function in UE4. What I’ve been trying to do for a few days is to allow the editor to modify sub-object pointers to components of an actor.

For example:

```

UCLASS(config=Game)
class AMyActor: public APawn
{
    GENERATED_BODY()
public:
    UPROPERTY(EditAnywhere, Category = Character)
    UMyComponent* MySpecialComponent;
};

```

It should be clear what I want to do here. Anytime a user in the editor creates a component on this actor of type UMyComponent, I want them to be able to point the property MySpecialComponent at it so that AMyActor can use it for special things. If this were possible, it would strike me as an intensely useful tool for giving users the ability to customize the actors that they make.

I would like this to work for both Blueprint archetypes as well as instances, but would be okay if it only applied to archetypes. As it stands, the property will only show up in the details panel at all if it’s an instance, and there is no way to edit those pointers. My foray into making some editor code to manually point them resulted in a variety of issues I believe related to [this](https://answers.unrealengine.com/questions/193073/47-uobject-custom-serialize-problems.html?sort=oldest) bug.

The whole thing has left me confused what the engine’s intent is for sub-objects and default sub-objects. CreateDefaultSubobject lets the class create, well, a default sub-object but the user can’t replace it with anything else?

The question extends to widgets and UMG. If I have a class that inherits UUserWidget I want the user to be able to specify pointers to sub-widgets. For example, if the user wants an image to receive, in code, something game-related, it would make a lot of sense for the user to be able to point a property in that UUserWidget to that sub-widget. Unity is _very_ seamless with stuff like this, allowing you to specify internal object references with no problem, allowing for a huge amount of editor customization, but I’m sitting here sort of lost in Unreal.

---

<div class="post-metadata">

**Author:** ![hiili](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hiili](https://forums.unrealengine.com/u/hiili)\
**Post date:** [October 13, 2015, 1:10pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/2 "2015-10-13T13:10:48Z")

</div>

I just stumbled upon this issue as well. Did you eventually find any solution to it?

---

<div class="post-metadata">

**Author:** ![hiili](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hiili](https://forums.unrealengine.com/u/hiili)\
**Post date:** [October 15, 2015, 3:21pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/3 "2015-10-15T15:21:04Z")

</div>

Ok, it seems that this kind-of works for archetypes in 4.9: assigning a component pointer in an archetype adds a new default subcomponent to that actor. However, it seems to be still impossible to a) make a pointer in an archetype to point to an existing default subcomponent, and to b) make a pointer in an instance to point to an existing subcomponent instance. I will open a new question for this.

_Edit_ [How to make component pointers assignable in the editor? - Programming & Scripting - Unreal Engine Forums](https://answers.unrealengine.com/questions/318521/how-to-make-component-pointers-assignable-in-the-e.html)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [October 15, 2015, 5:43pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/4 "2015-10-15T17:43:46Z")

</div>

Hey -

I’m not sure I understand what your goal is. Can you elaborate on what you’re trying to accomplish? When a pointer to a component is created in code the pointer has to be instantiated with the component to be created. Using the above code as an example, this is done by adding `MySpecialComponent = CreateDefaultSubobject(TEXT("MySpecialComponent"));` inside the class constructor. This is what will create the component as part of the actor and allow the properties of the component to be edited both inside the editor as well as in code. Below is another example of editing the component in code where I’ve created a sphere component and then set the radius of the sphere. This default value can then be overriden inside a blueprint if necessary.

```
    UPROPERTY(EditAnywhere, Category = Component)
        USphereComponent* MySphereComp;

MySphereComp = CreateDefaultSubobject<USphereComponent>(TEXT("MySphereComp")); 
MySphereComp->SetSphereRadius(453.0f);

```

---

<div class="post-metadata">

**Author:** ![hiili](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hiili](https://forums.unrealengine.com/u/hiili)\
**Post date:** [October 16, 2015, 8:24am UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/5 "2015-10-16T08:24:12Z")

</div>

Hi ,

Maybe it is best if I show a complete use case, so as to also give some context to this.

**Repro**

Create a new Basic Code project (no starter content needed).

Create a new Blueprint class that inherits from Actor. Let’s call it MyActor.

Create a new C++ class that inherits from Actor Component. Let’s call it Spinner.

Save and close the editor.

Add the following to the public section of Spinner.h:

```
UPROPERTY( EditAnywhere, BlueprintReadWrite )
UActorComponent * SpinTarget = 0;

```

Add the following to USpinner::TickComponent() in Spinner.cpp:

```
if( UPrimitiveComponent * primitiveTarget = dynamic_cast<UPrimitiveComponent *>(SpinTarget) )
{
	primitiveTarget->AddTorque( FVector( 0.f, 0.f, 1e6f ) );
}

```

Compile and relaunch the editor.

Open MyActor in the Blueprint editor.

Add two cube components; let’s call them Cube1 and Cube2. Move them a bit apart in the viewport. Enable Simulate Physics for both.

Add a Spinner component.

Go back to the level editor and drop a MyActor instance to the scene. Position it slightly above the floor.

In World Settings, tick Override World Gravity (so as to disable gravity).

You should now have something like this:

 ![62902-repro1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/5/8/e589a61df50da568acee35d300931caaadc16568.png)

 ![62903-repro2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/f/8/df8ed36963444f36332072302811fc027375e7ea.png)

Now, _in this case_, the user of the Spinner component could easily select the target component to be spun using the construction script of MyActor:

![62904-repro3.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/4/f/24f1e55ba33118647ee2eb8f953461e260bb06f7.png)

![62905-repro4.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/4/e/44e5801ce96a02bfff509afbece78ec2aed3035c.png)

However, this can become messy or plain impossible in more complex scenarios. At the same time, I do not see how to achieve the same by assigning the SpinTarget variable directly in the editor. The SpinTarget fields can be seen in the first two screenshots (marked with red). For the MyActor instance (first screenshot from the level editor), the drop-down list shows all Actor Component _classes_, instead of the Actor Component _instances_ present in the current actor. Nothing happens if I select one. For the MyActor archetype (second screenshot from the Blueprint editor), the drop-down list shows also all ActorComponent classes. If I select one, a _new_ component becomes created _in the instance_, instead of letting me to select one of the already existing cubes:

![62906-repro5.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/6/f/16f677ae9cb23701e93c2e4bf0cb897265a43a6f.png)

What is visible in the last screenshot is probably the intended behavior for creating new default subobjects.

However, my (our?) goal is to select and make the pointer to point at an _existing_ component, ie, Cube1 or Cube2 in this case, which I do not know how to do.

---

<div class="post-metadata">

**Author:** ![hiili](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hiili](https://forums.unrealengine.com/u/hiili)\
**Post date:** [October 16, 2015, 8:40am UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/6 "2015-10-16T08:40:50Z")

</div>

So, in essence, somehow replacing this drop-down list (don’t mind the differing variable names):

![62794-capture2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/8/9/a89878a9460b4cc20e8ca16eee60e770e6079c0d.png)

with an actor-picker-style selector box that would let me select between Cube1 and Cube2:

![62793-capture1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/1/8/e187161ba68202715d18ce97f9b9eab0a7008ea3.png)

Would solve the problem.

I am wondering whether there is a way to do this, or whether there is another approach to accomplish this, or is the current behavior a bug in the engine?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [October 16, 2015, 5:26pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/7 "2015-10-16T17:26:17Z")

</div>

The ability to create a pointer in code and set that pointer to specific blueprint component added in the editor does not exist, however I have entered a feature request (UE-22265) for this functionality.

In case it helps it is possible to have a pointer store a component during runtime. Rather than using UActorComponent you can use USceneComponent to create the pointer. Then if you get all of the components of an actor you can loop through the array for a specific component or choose which element number to store into the pointer.

---

<div class="post-metadata">

**Author:** ![vikhik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vikhik](https://forums.unrealengine.com/u/vikhik)\
**Post date:** [December 14, 2015, 5:03am UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/8 "2015-12-14T05:03:06Z")

</div>

Has this been completed since then? I am trying to let my designers assign a specific camera component in the editor rather than hardcoding which camera is being used.

I’m writing a new thread now - [Select component type in editor - Programming & Scripting - Unreal Engine Forums](https://answers.unrealengine.com/questions/346676/select-component-type-in-editor.html)

---

<div class="post-metadata">

**Author:** ![Dylan\_Barrie](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dylan\_Barrie](https://forums.unrealengine.com/u/Dylan_Barrie)\
**Post date:** [January 16, 2017, 8:20pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/9 "2017-01-16T20:20:46Z")

</div>

I am also trying to do the exact same thing here; in my case, I’m trying to create a component that can be added to an actor and which allows the user to select an instance of a `UMeshComponent` that _already exists_ in the parent actor.

---

<div class="post-metadata">

**Author:** ![Mr.tellino](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mr.tellino](https://forums.unrealengine.com/u/Mr.tellino)\
**Post date:** [September 3, 2017, 12:05am UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/10 "2017-09-03T00:05:34Z")

</div>

Unreal 4.13.2 user here. I don’t think this is exactly what you needed, but I figured out a way to have a pointer that points to an instantiated actor component. Here’s my situation:

I wanted a ComponentActivator component that would activate a specific component when being overlapped. What I did is that I added a TSubclassOf ClassOfComponentToActivate variable (editable anywhere) and a UActorComponent \*ComponentToActivate variable. Basically, after having added the ComponentActivator to the actor, the user specify a class to ClassOfComponentToActivate and then in the PostEditChangeProperty function I retrieve the component by using GetComponentByClass and store the result in ComponentToActivate. Obvisouly you have to make sure that a component of the specified class is present on the actor, but it’s not too big of a problem I think. Here’s the code:

```
////// ComponentActivator.h //////

UCLASS(ClassGroup = (Activation), meta = (BlueprintSpawnableComponent))
class ROBOT_API UComponentActivator : public UBoxComponent
{
	GENERATED_BODY()

protected:
	// Allows the user to specify the class of the component to be activated
	UPROPERTY(EditAnywhere)
	TSubclassOf<UActorComponent> ClassOfComponentToActivate;

private:
	// Pointer to the actual component to be activated
	// Will be set in the PostEditChangeProperty function
	UPROPERTY()
	UActorComponent *ComponentToActivate = nullptr;

public:
	UComponentActivator();

	// Called whenever a property on the component is changed
	virtual void PostEditChangeProperty(FPropertyChangedEvent &PropertyChangedEvent) override;
	
	UFUNCTION()
	void OnComponentBeginOverlapEvent(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
	
};

//////	ComponentActivator.cpp //////

UComponentActivator::UComponentActivator()
{
	bGenerateOverlapEvents = true;
	OnComponentBeginOverlap.AddDynamic(this, &UComponentActivator::OnComponentBeginOverlapEvent);
}

void UComponentActivator::PostEditChangeProperty(FPropertyChangedEvent &PropertyChangedEvent)
{
	Super::PostEditChangeProperty(PropertyChangedEvent);

	if (!ClassOfComponentToActivate)
	{
		ComponentToActivate = nullptr;
	}
	else
	{
		ComponentToActivate = GetOwner()->GetComponentByClass(ClassOfComponentToActivate);
	}
}

void UComponentActivator::OnComponentBeginOverlapEvent(UPrimitiveComponent * OverlappedComponent, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (!ComponentToActivate)
	{
		return;
	}

	ComponentToActivate->SetActive(true);
	ComponentToActivate->SetComponentTickEnabled(true);
}
```

---

<div class="post-metadata">

**Author:** ![Frozenfire](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Frozenfire](https://forums.unrealengine.com/u/Frozenfire)\
**Post date:** [December 13, 2017, 5:57pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/11 "2017-12-13T17:57:58Z")

</div>

Apparently CreateDefaultSubobject has multiple overload. I just tried and this seems to compile fine.

```
	TSubclassOf<UPCGameplayUnitComponent> TestClass;
	UPCGameplayUnitComponent* TestComp;
	TestComp = Cast<UPCGameplayUnitComponent>(PCIP.CreateDefaultSubobject(GetOuter(),TEXT("GameplayUnitComponent2"), TestClass, TestClass, false, true, true));
```

---

<div class="post-metadata">

**Author:** ![KircheschM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KircheschM](https://forums.unrealengine.com/u/KircheschM)\
**Post date:** [August 6, 2021, 5:29pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/12 "2021-08-06T17:29:20Z")

</div>

So, there is no way? what i am doing rn is setting the component tag, finding components with tag and selecting the component in code, which is ok, but i have to use tags, anybody found a better way?

---

<div class="post-metadata">

**Author:** ![Niklas\_Sommer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Niklas\_Sommer](https://forums.unrealengine.com/u/Niklas_Sommer)\
**Post date:** [August 19, 2021, 6:47pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/13 "2021-08-19T18:47:03Z")

</div>

Hey Guys,  
I think i found a good solution.

Header

```
UPROPERTY(EditDefaultsOnly, Category = "Camera|FirstPerson")
FName FirstPersonCameraParentName = "Mesh";

USceneComponent* FPAttachParent;

```

CPP

```
void ATP_ThirdPersonCharacter::BeginPlay()
{
	Super::BeginPlay();

	FPAttachParent = Cast<USceneComponent>(GetDefaultSubobjectByName(FirstPersonCameraParentName));
}

```

Now just specify the name you gave the component and you’re good to go.  
It also does not matter if the component was added in blueprint or in c++.

---

<div class="post-metadata">

**Author:** ![KircheschM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KircheschM](https://forums.unrealengine.com/u/KircheschM)\
**Post date:** [August 19, 2021, 6:51pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/14 "2021-08-19T18:51:02Z")

</div>

If you want to find by name you can use this:

```
class UNavigationComponent* Component;

UPROPERTY(EditAnywhere)
FComponentReference ComponentReference;

```

The component reference is basicly that, but it finds the component and you can even make a component picker if you are planing on using this to edit only instances

---

<div class="post-metadata">

**Author:** ![Niklas\_Sommer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Niklas\_Sommer](https://forums.unrealengine.com/u/Niklas_Sommer)\
**Post date:** [August 19, 2021, 7:06pm UTC](https://forums.unrealengine.com/t/editable-sub-object-component-pointers/314612/15 "2021-08-19T19:06:38Z")

</div>

Neat. Thank you!
