Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

AnswerHub Maintenance

Background maintenance is scheduled to occur between 9 - 11am EDT on Tuesday, May 21. Site operation may be slower than normal during this time and a brief interruption in operation may be observed

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 "

Product Version: UE 4.18
more ▼

asked Dec 11 '17 at 08:57 PM in C++ Programming

avatar image

1 1 4

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

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)

more ▼

answered Dec 20 '17 at 05:06 PM

avatar image

1 1 4

(comments are locked)
10|2000 characters needed characters left

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.

more ▼

answered Dec 12 '17 at 02:34 AM

avatar image

4.4k 32 77 40

avatar image Feztrath Dec 20 '17 at 04:58 PM

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

avatar image Jin_VE Dec 20 '17 at 05:10 PM

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.

avatar image Feztrath Dec 20 '17 at 05:22 PM

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

avatar image Jin_VE Dec 20 '17 at 05:34 PM

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.

avatar image Feztrath Dec 20 '17 at 06:20 PM

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

avatar image Jin_VE Dec 20 '17 at 11:32 PM

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?

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question