Creating Actors dynamically inside of CPP using TSubclassOf<> and NewObject<>

Is there a way to create child components or actors dynamically in a class using a separate blueprint actor class (with TSubclassOf<>) ?? The blueprint tutorial I’m trying to follow wants to Add Child Actor Components to a blueprint, using a separate blueprint class. The intent was that since the variable band blueprint can be resized dynamically in the level or in the editor, as you resize it, it would dynamically add more drop tiles when more could fit. So its trying to add child actors to the blueprint dynamically.

I have this class called “PVariableBand” that “PDropTargetSet” inherits from. I also have a class called “PDropTarget” (inherits from the Actor class). Inside of “PDropTargetSet.h” I have a variable TSubclassOf TargetComponentClass.

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Components")
	TSubclassOf<class APDropTarget> TargetComponentClass;

This is so when I create the child blueprint class, I can pick from the drop down menu anything I want to spawn dynamically at runtime.
Then inside of the .cpp file, Im trying to do spawn this blueprint actor using "APDropTarget* NewDropTarget = NewObject(this, TargetComponentClass->StaticClass());

APDropTarget* NewDropTarget = NewObject<APDropTarget>(this, TargetComponentClass->StaticClass()); 

but its throwing an error message at me. Is this the correct way of creating child components or child actors dynamically? I’m getting the exact same error message as a previous poster but his solution didnt work for me. If anyone could help me out with this, I’d be extremely grateful. Will continue to plug away at it

Links:
Illustration of what I’m trying to do: Trying to add BP_DropTarget dynamically to BP_VariableBand - Album on Imgur
What the tutorial was trying to do: What the tutorial was trying to do - Album on Imgur
Crash Error: UE4 Crash Error - Album on Imgur
This post is close to what I was trying to achieve but his solution didnt work for me: NewObject<> create from TSubclassOf

PDropTargetSet.cpp

#pragma once

#include "CoreMinimal.h"
#include "PVariableBand.h"
#include "PDropTargetSet.generated.h"

/**
 * 
 */
UCLASS()
class PINBALLGAME_API APDropTargetSet : public APVariableBand
{
	GENERATED_BODY()

public:

	APDropTargetSet(); //constructor

	virtual void OnConstruction(const FTransform& Transform) override;
	
protected:
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category ="Display")
	FString DropTargetString;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Components")
	TSubclassOf<class APDropTarget> TargetComponentClass;
};


"PDropTargetSet.h"

type
#include “PDropTargetSet.h”
#include “PDropTarget.h”

APDropTargetSet::APDropTargetSet()
{
DropTargetString = “Pinball”; //setting default value
}

void APDropTargetSet::OnConstruction(const FTransform& Transform)
{
Super::OnConstruction(Transform); //MUST CALL PARENT FUNCTIONALITY

if (!DropTargetString.IsEmpty()) //If its not empty
{
	DropTargetString.ToUpper(); //convert all text to upper case
	TArray<TCHAR> DropTargetStringArray = DropTargetString.GetCharArray();
	for (TCHAR characters : DropTargetString)
	{
		//APDropTarget* NewDropTarget =
		APDropTarget* NewDropTarget = NewObject<APDropTarget>(this, TargetComponentClass->StaticClass()); //creating new object using TSubclassOf<> variable
		if (NewDropTarget)
		{
			NewDropTarget->AttachToActor(this, FAttachmentTransformRules::KeepRelativeTransform);
			UE_LOG(LogTemp, Warning, TEXT("Attempted to add blueprint"));
		}
	}
}

//More  functionality

} or paste code here

Unreal Engine has it’s actors contained and managed by UWorld.
Each AActor may have UActorComponent(s) attached to it.

To create actor, you need to get reference to world and call

UWorld* world = GetWorld();
world->SpawnActor<APDropTarget>(TargetComponentClass, FTransform(...), FActorSpawnParameters(...));

Where FTransform is where, which size, how rotated actor will be placed and FActorSpawnParameters contain metadata for it(Name, flags, etc), i believe it can be ommited if you don’t need it.

WARNING: SpawnActor function should be called in functions, that happen in gameplay time withing lifecycle of a world. You can’t do it in Constructor or OnConstruction!

To create a component you need to create an UPROPERTY variable for it (eg. class UMyComponent* myComponent)

Creation of components is possible only inside of constructor!

myComponent = CreateDefaultSubobject<UMyComponent>(TEXT("NameOfComponent"));

You can’t create a Component out of an Actor. Components start with U and Actors start with A.