UE4 : GetPlayerState() doesn't work in PlayerController code (template version is called)

I’m trying to get PlayerState from APawn class, which worked fine in my MyCharacter.cpp code.
But It doesn’t work, in MyPlayerController.cpp file, which is super weird to me.



My code goes like this :

auto MyPlayerState = Cast<AMyPlayerState>(GetPlayerState());

And GetPlayerState() above is well defined in APawn.h, like this :

/** If Pawn is possessed by a player, returns its Player State.  Needed for network play as controllers are not replicated to clients. */
APlayerState* GetPlayerState() const { return PlayerState; }

But whenever I tried to execute the same code in MyPlayerController.cpp, it cannot find matching overload function, instead it gets me this :

/** Templated convenience version of GetPlayerState. */
template<class T>
T* GetPlayerState() const { return Cast<T>(PlayerState); }

Why on earth it tries to call template version of GetPlayerState() in MyPlayerController.cpp, while it works just fine in MyCharacter.cpp file?

Is there any other way to enforce GetPlayerState() not to use template version of GetPlayerState()?

I tried to fix my code to GetPawn()->PlayerState(), but this time, unreal engine crashes when game is played…

Just use #include “GameFramework/PlayerState.h”
And than GetPlayerState() will work normal.

Correct include is important. I was in same situation.
P.S.: Sorry for my English. I hope than can help you

1 Like