Why do I have to include PhysicsHandleComponent above CoreMinimal?

Hi! When I’m declaring a UPhysicsHandleComponent in Grabber.h I have to include “Runtime/Engine/Classes/PhysicsEngine/PhysicsHandleComponent.h”. When I include it below “CoreMinimal.h” VS doesn’t recognize UGrabber as a class name. See screenshot, it explains it better.

After I move include above “CoreMinimal.h” errors are gone and it works. Could someone explain it?

2 Likes

I’ve struggled with this as well. Fortunately I’m surrounded by a friendly bunch of UE4 programmers - I asked one of them what the hell was going on with the order of the includes.

Basically, the wavey red error line is not to be trusted. It’s a good indication of issues, but it’s a bit flawed when working with UE4 - Visual Studio doesn’t always understand what’s going on. If you build / compile, your game should still run (provided you haven’t got any additional errors). My wavey red line normally sorts itself out at this point.

It’s a pain in the bum, I agree, but something you just have to get used to it seems.

During this discussion, it was also noted that includes should be kept in the .cpp file unless they need to be in the header file. I had been putting them in the header file thinking that it was cool, because the .cpp file includes everything from the header file anyway. This is bad for performance apparently.

edit Just to clarify - I’m not saying that you should put #include “PhysicsEngine/PhysicsHandleComponent.h” in the cpp file in this case, as it’s referenced in the header file. Just a general rule to avoid putting includes in the header file unless required :slight_smile:

1 Like

Thanks for great answer!

I usually don’t put includes in header files - but thanks for a reminder. I totaly forgot (coming from Java) it is not a good practice :slight_smile:

1 Like

Privacy & Terms