How to access camera component in Player Controller?

I want to access the camera in controller class, how to do it?

MyController.cpp

ACharacter* Character{};
AMyCharacter* MyCharacterRef{};
Character = UGameplayStatics::GetPlayerCharacter(this, 0);
MyCharacterRef = Cast<AMyCharacter>(Character);
FVector CamLocation = MyCharacterRef->Camera->UCameraComponent->GetComponentLocation();

I got an error pointer to incomplete type is not allowed. MyCharacterRef

You need to include the header for your MyCharacter class in your MyController source file.

1 Like

already included, but still i am unable to access the character camera

If you are correctly including the header, then you will not get that error.

How about you post a copy-and-paste of the actual error message text?
And perhaps copy the #include parts of your MyCharacter.cpp file and paste it as a code block, too?

Mycontroller.h
#include "AllCharacters/MyCharacter.h"

Mycontroller.cpp
class AMyCharacter

Error (active)|E0393|pointer to incomplete class type is not allowed

Shoudn’t it be

Include in Mycontroller.cpp
#include "AllCharacters/MyCharacter.h"

Forward declaration in Mycontroller.h
class AMyCharacter;

1 Like

It’s not clear which class it’s complaining about.
It may be complaining about the camera reference, or the camera component.
You need to include the headers for all the classes your pointers will travel through.
Thus, the type of the field “Camera” needs to be known (included) and the type of the field “UCameraComponent” of THAT type also needs to be known.
(Which is kinda weird, btw – most of the type, fields aren’t named the same as the class of the value of the field.)

This is all plain C++ language questions; you may actually have better luck asking in a C++ specific forum. Nothing about these rules are specific to Unreal Engine. (Although, of course, the specifics about how Unreal lays out its headers are specific to Unreal, and if you ask in a language forum, they might in turn ask about that to be able to answer …)

1 Like

The problem was because of incorrect casting to MyCharacter class.

Solution:
AMyCharacter * MyCharacterRef = Cast<AMyCharacter>(GetPawn());