Proper scene components declaration

When looking at this tutorial I don’t understand why USceneComponent is declared at header but UCameraComponent is declared and initialized in constructor. Wouldn’t it make more sense to declare all scene components at header and initialize them at constructor so header can give a good overview of class structure without digging .cpp? I’ve tried to declare UCameraComponent* at header, but this class is not defined here. Is this a bad practice to include appropriate header for camera component in my class header not in .cpp?

I would say that it’s best practice to declare all components in the header that you want to be able to modify after construction, such as if you want to be able to change camera field of view at a later stage, then it’s better to declare it in the header. Otherwise it would be difficult retrieving a reference to the object that you want to modify without having a variable as a reference.

Regarding including the appropriate header file for the camera component. Since the UCameraComponent* variable is a pointer, you should simply forward declare using the class keyword before the variable declaration like so:

class UCameraComponent* MyCamera;

alternatively place it before the class declaration like so:

class UCameraComponent;

UCLASS()
class SOME_API ACustomActor : AActor
{
    GENERATED_BODY()
public:

    UCameraComponent* MyCamera;
}

That way you tell the compiler that you want to reserve space in memory for the pointer for use at a later stage. And then when you want to utilize the UCameraComponent you include the header file in the .cpp file like normal.

~Per “Gimmic” Johansson

Thank you for a very useful answer, sir!