Accessing member variables from a child class

Inside a child class AAICharacter (inherits from a class I made, APlayerCharacter, which inherits from ACharacter), I’m trying to access a UPaperSpriteComponent pointer declared in APlayerCharacter.

My Sprite Component declaration:
APlayerCharacter.h

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Player", meta = (AllowPrivateAccess = "true"))
        class UPaperSpriteComponent* PlayerSprite;

This is the line of code I need to work in my cpp
AAICharacter.cpp

 FVector SpawnLocation = PlayerSprite->GetSocketLocation("Ball");

This is the error the IntelliSense is giving me on PlayerSprite.
image

The pointer is protected so I should have access to it. I’m still new to how inheritance works in UE5

Hi @TheCrackGoblin,

if you want to access a member variable from your APlayerCharacter, you can easily do this by:

  1. Make sure the variable you want to access is in the public section OR you have a getter to get your protected variable (which is better approach I believe).
APlayerCharacter.h

public:
// Getter for Example Variable
float GetExample();

protected:
// Example Var
UPROPERTY(...)
float Example;

APlayerCharacter.cpp
float APlayerCharacter::GetExample()
{
   return Example;
}

  1. From your AAICharacter class, you can get the character first before getting the variable
AAICharacter.cpp (I Will use it in begin play for example)

// Gameplay statics needed to get your character pointer
#include "Kismet/GameplayStatics.h"

// TODO: Also include your character, and in your case, also include your PaperSprite header file

void AAICharacter::BeginPlay()
{
...
APlayerCharacter* Char = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
if(Char)
{ // Make sure Char variable is valid
  Char->GetExample();
}
}

you can try this to access the member variable from your character. Hope this help

this is not accessing the member that is the issue:
in C++ protected allows for Child and friend (these get cumbersome) classes access as though they were public, while classes that are not derived from or, “fried of” is treated as private.

this is not an Unreal thing this is a C++ thing

the error you are getting is to do with includes specifically, and tangentially about inheritance

my guess is that you have

PlayerCharacter.h

class APlayerCharacter : ACharacter

then in your AAICharacter.h

#include "PlayerCharacter.h"

class AAICharacter : public APlayerCharacter

when you inherit like this the Header, and the Header only gets copy and pasted wherever the #include *** so if that file being included doesn’t include all the stuff that will be needed to deal with the class then those things need to be included as well.

where you are forward declaring the UPaperSpriteComponent when you do “class UPaperSpriteComponent” in the PlayerCharacter.h, and then only doing the actual include in the PlayerCharacter.cpp.

you need to also include the header for the PaperSpriteComponent in your AICharacter.cpp or if the type is integral to your design move the include to the PlayerCharacter.h where everything dealing with the APlayerCharacter will probably need to understand what a UPaperSpriteComponent is and does.

the advantages of forward declarations is to prevent recursion, or potentially infinite recursion in the Header compilation pass of the compiler (A needs B, B needs C, and C needs A this include structure could easily lead to to infinite recursion), so we use forward declaration to prevent potentially this recursion nightmare.

when the forward declarations fall down is when everything in a chain needs to know about, and how to communicate with a class, forward declaring it will lead to more includes then just putting the include in the highest of the hierarchy

Ah I misunderstood the question, I thought access member variable from another class, my bad. Also thanks for correcting @gardian206

1 Like