'Nested' Includes between a custom pawn and a custom component

I’m trying to set up a UActorComponent for a custom pawn. This custom pawn, however, would also like a reference to its owner - which will always be the custom pawn.

Accordingly in my CustomPawn.h, I #include "CustomComponent.h" to make the custom component a variable, UCustomComponent* CustomComponent.

However, in CustomComponent.h, in order to have a ACustomPawn* MyOwner, I need to #include "CustomPawn.h"

This results in compiling errors, specifically an error C2143: syntax error: missing ';' before '*' on my UCustomComponent* CustomComponent line. I understand that it has to do with two headers including eachother; however, all I’m getting is that I need to use #ifndef #define, unless I have #pragma once, which UE4 by default uses.

What do you all suggest?

Hi Kowbell!

You can access component’s owner by GetOwner() method. It will return direct parent of component, I mean if component is attached to another component then GetOwner() will return that parent component. But in your case I think component is parented by actor itself, so you can use GetOwner() instead storing MyOwner property.

If you want to avoid recursive including, then you can use forward class declaration. Eg:

CustomPawn.h:

class UCustomComponent* CustomComponent;

CustomPawn.cpp:

#include "CustomComponent.h"

CustomPawn::CustomPawn()
{
  CustomComponent = ...
}

You can find similar examples in default projects templates (and in many other places)

I was hoping to store my owner as the custom pawn class because in several different parts of the program I wanted to reference variables specific to that class, and casting to that class every time via GetOwner() would get very tedious. Out right now, I’ll try that out when I get back.

Edit: That worked! Thanks!