Dynamic UActorComponent in C++

UPDATE: SOLVED
I’m trying to set up my player pawn such that the player can select preferences from their main menu which will help build the player pawn, for instance a movement attribute. I derived the parent movement classes from UActorComponent, BaseMovement.cpp, and two C++ derived children children TeleportMovement.cpp and ContinuosMovement.cpp.

I am having trouble setting the UActorComponent class to create, in the player pawn class I have the following:

//Pawn Constructor
    MovementComponent = Cast<UMovementComponentBase>(CreateDefaultSubobject(TEXT("Movement Component"), MovementComponentClass, nullptr, false, false));
    if (MovementComponent) { this->AddOwnedComponent(MovementComponent); }
    if(MovementComponent == nullptr) { UE_LOG(LogTemp, Warning, TEXT("Movement is null")); }

And in the header file I have:

protected:

UPROPERTY(EditAnywhere, category = "Movement")
class UMovementComponentBase* MovementComponent;

UPROPERTY(EditAnywhere, BlueprintReadWrite, category = "Movement")
TSubclassOf<class UMovementComponentBase> MovementComponentClass;

I set the MovementComponentClass variable in the blueprint class of the player pawn. However, the UE log displays that the MovementComponentClass is NULL on construction. I have tried using moving the MovementComponentClass around and using different UPROPERTY arguments to no avail. Does anyone know how to implement UActorComponents dynamically or get the TSubClass variable to appear not null?

**Update
I have tried calling NewObject in Begin play as seen below, however the game crashes and tells me: “NewObject called with a nullptr class object”.

MovementComponent = NewObject<UMovementComponentBase>(this, MovementComponentClass);

I don’t understand why the class object is NULL, I set it in blue print under the details panel and my a blueprintimplementable function that sets the class and called it the line before I call new object, and it’s still NULL.

2nd update:

I have tried the following and got the following error on crash: “NewObject called with invalid class, Class must be a child of MovementComponentBase” Which baffles me considering MovementComponentClass is a TSubclassof MovementCompoentBase.

MovementComponent = NewObject<UMovementComponentBase>(this, MovementComponentClass->StaticClass());

SOLVED:

I guess during the tweaking of my class, the blue print became variable became unset, I reset it and my second implementation worked:

  MovementComponent = NewObject<UMovementComponentBase>(this, MovementComponentClass);

phew

Solved:

MovementComponent = NewObject(this, MovementComponentClass);

Big help on this post: https://answers.unrealengine.com/questions/959664/view.html