# CreateDefaultSubobject with SubClassOf

**URL:** <https://forums.unrealengine.com/t/createdefaultsubobject-with-subclassof/2020878>\
**Category:** C++\
**Tags:** constructors, unreal-engine, CPP, question\
**Created:** [September 14, 2024, 5:15pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-with-subclassof/2020878 "2024-09-14T17:15:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AreYouTheBot020](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AreYouTheBot020](https://forums.unrealengine.com/u/AreYouTheBot020)\
**Post date:** [September 14, 2024, 5:15pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-with-subclassof/2020878/1 "2024-09-14T17:15:09Z")

</div>

I have a `UMyActorComp : UActorComponent` that I am finding I need to do stuff with as a Blueprint, I should only need 1 Blueprint Type, and “shouldn’t” need to derive further.

currently I have

```auto
UPROPERTY(EditAnywhere, BlueprintReadOnly)
TObjectPtr<UMyActorComp> ActorComp;

AMyActor() 
{
    ActorComp = CreateDefaultSubobject<UMyActorComp>(TEXT("ActorComp"));
}

```

then I start doing work with it during `PostInitializeComponents()` (passing pointers, and registering it to relevant subsystem managers)

so because I now need a blueprint version

```auto
UPROPERTY(EditDefaultsOnly)
TSubclassOf<UMyActorComp> ActorCompBlueprint;
UPROPERTY(EditAnywhere, BlueprintReadOnly)
TObjectPtr<UMyActorComp> ActorComp;

AMyActor()
{
    if( ActorCompBlueprint == nullptr )
    {
		UE_LOG(LogTemp, Error, TEXT("Set ActorCompBlueprint for %s"), *GetName());
		ActorCompBlueprint = UMyActorComp::StaticClass();
	}
	ActorComp = CreateDefaultSubobject<UMyActorComp>(FName(TEXT("ActorComp")), UMyActorComp::StaticClass(), ActorCompBlueprint.Get(), true, false);
        }
}

```

this throws an error at code analysis:

> "no instance of overloaded function “AMyActor::CreateDefaultSubobject” matches the argument list argument types are: (FName, UClass\*, UClass\*, bool, bool)  
> **when this is overload 1.**

new Object in the constructor `ActorComp = NewObject<UMyActorComp>(this, ActorCompBlueprint.Get(), TEXT("ActorComp));` crashes

supposedly the suggestions I have found is use `NewObject` during `PostIntializeComponents()` which I need the object to exist by that point (assigning pointers to, and trusting it to be a valid pointers so they can be registered to relevent subsystem managers and there is no hitch right at BeginPlay)

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [September 14, 2024, 5:33pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-with-subclassof/2020878/2 "2024-09-14T17:33:51Z")

</div>

If you want to use a blueprint component then you’ll have to add it in blueprint, and then I suppose you can do GetComponentByClass in PostInitializeComponents if you want to access it through C++

---

<div class="post-metadata">

**Author:** ![AreYouTheBot020](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AreYouTheBot020](https://forums.unrealengine.com/u/AreYouTheBot020)\
**Post date:** [September 14, 2024, 6:01pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-with-subclassof/2020878/3 "2024-09-14T18:01:10Z")

</div>

this works, I hate it, but it works, needed an error message and forced return in `PostInitializeComponents()` to prevent a crash.

used `FindComponentByClass<T>()` where it has the cast built in.

```auto
void AMyActor::PostInitializeComponents()
{
    Super::PostInitializeComponents();
    if( ActorComp == nullptr )
    {
        if( !(ActorComp = FindComponentByClass<UMyActorComp>()) )
        {
			UE_LOG(LogTemp, Error, TEXT("ActorComp unassigned for blueprint of %s"), *GetName());
			return;
        }
    }
}

```

---

<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:** [October 14, 2024, 6:02pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-with-subclassof/2020878/4 "2024-10-14T18:02:03Z")

</div>

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