Initializing my PhysicsHandle crashes the editor

Hello all!
I’m not really sure what’s causing the problem. As soon as I do:

PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();

and build the solution, either through VS or the Editor it self, as soon as it finishes building my Editor crashes.
Not when I press play or anything, but as soon as it finishes building. I remove that line of code and everything runs fine again. I have it defined in Grabber.h file and set the value to nullptr. Any help is appreciated!

EDIT 1:
So far I’ve been able to find out that the problem is caused by calling the GetOwner() method. As to why that is actually a problem, without really doing anything with the component, and why it crashes my editor on loading it up stays a mystery to me.

EDIT 2:
Gave up on trying to figure it out as to why it is actually happening, the other components that we used had GetOwner() in their .cpp files and it did not crash the editor. So bottom line is, I did a checkout, deleted all the derived files and just rebuilt them and regenerated all project files. Put in the code the same way as before AND it started all working again as intended.

Moral of the story, do yout commits as often as you can, you never know when things can break in the most peculiar of ways…

The GetOwner() method cannot be called inside the constructor. Call it in BeginPlay() instead.

As a matter of fact, in Unreal you should always avoid putting code in the constructor when it could go in BeginPlay(). That’s because the UObject system constructs objects in non-gameplay scenarios, for example when creating the so-called Class Default Objects (CDOs). So, putting code in UObject constructors requires a deeper understanding of when and why the engine might internally create instances of that UObject.

1 Like

Privacy & Terms