Hello Unreal-ites (I’m sorry this is long, but I like to provide detail, not filler),
[My C++ Experience Level]
Now that I have enough Blueprint experience under my belt and I am content with what I have created, I began to delve into Unreal C++. I have some standard C++ experience and understand what and how to use pointers, dereferences, address-of, forward declarations, includes and why we need them, const, among other things.
I’d like to know if my assertion of something is true or not (below)
In the Battery Collector tutorial series (https://www.youtube.com/watch?v=EIpt…_dGiM4&index=3), we begin to define and implement a BatteryCollector class. This series was made many Unreal versions ago, and so, many things have changed. One thing that changed was at 12:05, the author types “RootCompoent = PickupMesh” in Pickup.cpp in the class default constructor.
[Things Have Changed]
Back then, this statement did not make Visual Studio question the assignment. However, somewhere between then and now, the assignment operator contains a squiggly under it stating: “A value of type "UStaticMeshComponent” cannot be assigned to an entity of type "USceneComponent** " ".
A commenter suggested; some years after this video, UStaticMeshComponent became a distance child of USceneComponent, and the squiggly can be safely ignored. I’m not happy with that because there is no answer as to why. My guess is because this is not a syntax error and will allow to start compilation, and, after compilation, the linker links, though the hierarchy, a UStaticMeshComponent type to a USceneComponent Type. * IS THIS A CORRECT UNDERSTANDING?*
If we use the include statement *#include “Components/StaticMeshComponent.h” *in Pickup.cpp, as another commenter suggested, this makes the squiggly go away. Here is my why assertion, am I correct?
The variable in Pickup.h called PickupMesh is a type of UStaticMeshComponent AND is declared using a FORWARD declaration, hence why #include “Components/StaticMeshComponent.h” statement was not necessary in Pickup.h.
StaticMeshComponent.h states the class inherits from UMeshComponent.
MeshComponent.h states the class inherits from UPrimitiveComponent.
PrimitiveComponent.h states the class inherits from USceneComponent
Since the linker is able to successfully link a UStaticMesh type up to USceneComponent type, (PickupMesh type up to a RootComponent type), the include statement is NOT necessary because the linker establishes the link [upward]. IS THIS A CORRECT UNDERSTANDING?
If we place the include statement in the Pickup.CPP file, somehow, Visual Studio is able to use some pre-compile/pre-linker determination, that, yes, a UStaticMeshComponent IS a USceneComponent (which makes the squiggly go away). Is that fair?
I do understand that two reasons to use forward declarations is to speed up compile times and help break dependencies created by include statements. If this is true, then I don’t understand why use forward declarations if you need to use an #include statement in the CPP file anyway. What am I possibly missing?