Download

UPROPERTY issue

Having a question about UPROPERTY. I do not have a very good grip on C++ yet so this might be a basic question.

The pawn tutorial mentions:


UPROPERTY()
USceneComponent* OurVisibleComponent;

It is written in the Pawn.h file. I would like to have the same functionality with the camera attached to the pawn, however that camera is created in the pawn.cpp like this:


	// Create a camera and a visible object
	UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));

In order to get the functionality I attempted to clone what was done with


UPROPERTY()
USceneComponent* OurVisibleComponent;

And added:


UPROPERTY()
UCameraComponent* OurCamera;

in pawn.h

It appeared to compile, but the compile time went from 9 seconds to 100 seconds.
Now with my limited grasp on C++ so far,

  1. Why is the visible component being declared in .h and the camera in .cpp?
  2. Was defining the camera in the .h “bad” or breaking something because it added 90 seconds of compile time?

Changes to headers generally result in higher compile times than changes to .cpp files. Any file that included the changed header will also need to be recompiled. This is especially noticeable in UE4 cause it’s generating code based on your headers. This isn’t bad, just annoying, try to get your headers right the first time and you’ll be right.

I can’t speak to why the camera is only being declared in the cpp file, it must be some design decision of the tutorial writer.

It appears that the camera is simply being generated at runtime then attached to the root component. The engine should be able to keep track of it from there, but you wouldn’t have a pointer to it should you need access to the camera component. If you look at the 3rd person template, the camera is declared in .h and defined in .cpp just like the visual component is, leaving a pointer for further modification.

Like Karl said, it appears to have been a design decision by the tutorial writer. They must not have expected to need access to that camera again. Personally, I would have done it the same way you did.