Engine Crashes when creating certain SubObjects or Objects in Player Controller

Hello, im having quite a dilema here when i try to create a subobject in my player controller

using the syntax

 UPawnnMovementComponent *MyMovmentComponent = CreateDefaultSubobject<UPawnMovementComponent>(TEXT("MyMovementComponent"));

or the syntax

UPawnnMovementComponent *MyMovmentComponent = GetPawn()->CreateDefaultSubobject<UPawnMovementComponent>(TEXT("MyMovementComponent"));

or a new Object using the syntax

UPawnMovementComponent *MyMovementComponent = NewObject<UPawnMovementComponent>( GetPawn(), TEXT("CustomMovementComponent"));

or the syntax

UPawnMovementComponent *MyMovementComponent = GetPawn()->NewObject<UPawnMovementComponent>( GetPawn(), TEXT("CustomMovementComponent"));

All of those crashes when i hit Play in the editor, however i tested with other components like USpringComponent and UStaticMeshComponent , and they worked, as well as my custom MyPawnMovementComponent with NewObject this last one crashed when creating it as SubObject.




I may be misundersating how CreatedefaultSubobject Works, so i would like to know why it crashes, and what can i do to create a PawnMovementComponent object in my “player controller” and pass it to my " possesed pawn "

You have a custom controller class?

From what I see in the docs, the string you pass into CreateDefaultSubobject function is just the name that will be given to the new object, not the name of the class to create an instance of. It looks like the template argument is where you specify which class to create. Given the Cast template, that’s a bit counter-intuitive – or I’m misunderstanding the docs. But since no one has come to help you in five hours, I thought I’d at least try.

thank you for your answer and yes i have a custom controller class, and even thought the string name is different i used them as the name for the new object rather than the class name, however i finally solved my problem due to this small and angrying feature no one was close to answer it (i mean my teachers), PawnMovementComponent inherits from UMovementComponent which is an Abstract Class, and “Abstract classes” cannot be instanced, thus giving me error and crashing everything (though not sure why the crash rather than an error at compile time) here you can take a look at the remarks section

I Finally solved my problem as it turns out UPawnMovementComponent inherits from UMovementComponent which is an Abstract Class, and “Abstract classes” cannot be instanced, thus giving me error and crashing everything (though not sure why the crash rather than an error at compile time)

I think that’s kind of what I meant. You need to put your class name in the template parameter and the name of the variable between the quotes.

oh if you meant the third and fourth parameter also crashed when i tested it, either adding the return type or the return type and classToCreateByDefault parameter both crashed still thats why i omited them from the code above XD

I assumed you had your own movement component class, derived from UPawnMovementComponent. In that case, I was saying, your creation code should look like this

UMyMovementComponent *MyMovementComponent = GetPawn()->CreateDefaultSubobject<UMyMovementComponent>(TEXT("MyMovementComponent"));

But then again, you’d want to assign that instance to a member variable of the owning actor, so it’s not quite correct. I was just going off of what you had posted so it looked familiar.

sorry i got a bit confused so you meant that is not working beacuse is trying to assign it to my player controller an thus crashed due to being unable to do it, am i right?

thats what you meant with “it’s not quite correct”, rigth?

then why is that it doesnt work since it is an actor and its not an abstract class anymore?

im quite new to unreal so sorry if it seems im not getting it

The code you posted shows local variables being used. If you create a component you need to have a pointer to it so you need to have a member variable in your actor that holds it. A local variable will disappear when the function is completed. That’s what I meant by “not quite correct”. That isn’t why it’s breaking.

You said it’s breaking because you can’t instantiate UPawnMovementComponent. That it’s an abstract class. If that’s the case then you may need to create your own class that is derived from UPawnMovementComponent. If you implement the missing functions then it won’t be an abstract class. Then you can use the format in my example – except, the “MyMovementComponent” variable should be a member of your class (a UPROPERTY)…

Or there may be a component that is derived from UPawnMovementComponeent which you can use out of the box. What are you trying to do exactly?

Another thing: movement components usually go in the Pawn, not the controller. Why are you doing this with the controller?