Casting Pawn To PlayerController

Hi,

I am not understanding how is it possible to cast a APawn to APlayerController. They both share AActor in their hierarchy. I thought you can only cast to parents or children. And it does not seem that APawn is a child nor a parent of APlayerController. Can someone help me please understand this? This is part of Video “Setting Timers In C++” for shooter C++ game.

I haven’t seen the lecture yet put you are right about casting. A player Controller is actually used to control one Pawn But You can get the Pawn from the Controller that is attached to it…

It’s not. The line is

APlayerController* PlayerController = Cast<APlayerController>(PawnKilled->GetController());

Which first calls GetController on the pawn which returns an AController which is then casted to APlayerController

2 Likes

You are correct. I was taken aback by that statement and was researching this. But today, I finished the video and the teacher did correct it by the end of the video exactly as you are saying. It makes sense. It also is a danger of C++ as it will not complain and just give you a nullptr.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms