Unreal Engine generates following function:
void AFlyingPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
//stuff...
}
Notice the "class" specifier before the parameter's type. What does it mean?
As you might know, Unreal manages multiple implementations of the same base classes to define a common ground. Every developer must, then, create child classes from the ones the engine has to offer in order to perform tasks within the Engine.
In this case, it's about an InputComponent, which is used to handle User Input, interpret it and pass it along to Controllers and/or, subsequently, Pawns.
For example, if you want to define elements such as Pawns, PlayerControllers, AIControllers, the HUD and the like, you do so in a GameMode you then configure into the Project Settings or, directly, into the level through the World Settings (in case your level needs a specific GameMode). Those references are classes as well, which will be instantiated by the Engine in due time to setup the Game.
With this in mind, here comes the downside. In UE4 C++ (yeah, it's a thing!), since the engine ties the loose ends, sometimes you won't be able to use certain classes because they're not declared. Of course, you can include them but think about it: how many circular dependencies will be created if you make all the inclusions you need for one class only to find another one might indirectly require that one?
The solution is Forward Declaration. This case, however, is a special flavor called Shorthand Forward Declaration in which you declare a type in the exact place where you use the class.
This is extremely handy if you're just using it once, so you don't end up with a terrible list of declarations at the start of your file.
For example, should you want to know the current default Pawn class defined, you can check the GetDefaultPawnClass
public variable in your GameMode (Let's call it MyGameMode
). The variable is defined as follows:
TSubclassOf < APawn > DefaultPawnClass
See that TSubclassOf
? That's actually a Class Template to ensure type safety. It's actually a hint to the Editor to show you only classes derived from APawn
.
Should you use a custom type and based in what I've been discussing so far, you could find stuff like this:
TSubclassOf<class ASpeedyPawn> MySpeedyPawn;