GetControlledTank() from different .cpp files

Why is it when we call GetControlledTank() from within TankAIController.cpp, we get a non-player tank, but when we call the same GetControlledTank() function from within TankPlayerController.cpp, we get the player tank. The function is defined the same way in both files. What causes the program to differentiate what kind of tank pawn to find in each case?

That’s what the purpose of the controller classes are for. The TankAIController will be controlling AI spawned tanks whilst the TankPlayerController will be controlling player controlled tanks which would just be you.

I think because when we start the game unreal uses the GameMode. In the GameMode we set our player controller “TankPlayerController” and our default pawn class “BP_Tank”.
The tanks you placed in the world have their Controller Class set to “TankAIController”.
Calling GetControlledTank() in either controller class just returns a ATank*, which just point to that specific BP_Tank the controller class is attached to.
It’s not really a different kind of tank pawn, both AI and Player use BP_Tank, it’s just that the one from our is spawned for us by the GameMode. The ones you placed in the world are there already with the TankAIController attached.

Privacy & Terms