Casting to PlayerState crashes UE4 editor

I have been attempting to cast to playerstate to call a custom method in my defined player state, however, it constantly crashes the UE4 editor whenever the method with the cast in it is called.

I have isolated it to these two lines:

AMyPlayerState* state = Cast<AMyPlayerState>(UGameplayStatics::GetPlayerController(GetWorld(), 0)->PlayerState);

I have also tried

AMyPlayerState* state = Cast<AMyPlayerState>(UGameplayStatics::GetPlayerController(GetWorld(), 0));


Any help would be greatly appreciated.

EDIT: I should also note (for clarity/to rule out any doubt) that the project does indeed compile.


Your first example looks correct to me, but I’m not sure if that check() from the Assertions tool is suitable for checking pointers for gameplay purposes?
If you check your pointer with if(state != nullptr) before using it, is ‘state’ valid after the cast?
Do you have your custom PlayerState set to be used in the GameMode?

The second code sample should never work, you can’t cast a playercontroller to a playerstate.

The first example should be broken down more so you can determine where the nullptr is coming from. It could be coming from either one of GetWorld, GetPlayerController, ->PlayerState or the cast.

RotemS is right, “check()” crashes your editor cause you provide a ptr that’s not valid.
You may also check if the class type of PlayerState you got is actually a sub class of AMyPlayerState, or Cast<AMyPlayerState> will fail and give you a null ptr.

I checked that and the pointer is not valid, yes I have a gamemode assigned.

Oddly enough, it did somehow compile and only failed when the method was called. I broke it down and all the components are valid but somehow the state pointer is not.

Thank you for letting me know! I removed/replaced ‘check()’ from my project. I did have the player state set as a derived blueprint but setting it to the class itself did not solve it unfortunately.

Is it worth noting that this is being done in the destroy() function? The whole point is that when the actor (not player obviously) is destroyed,information gets stored in the player state via my custom method in the player state I created (method is obviously not called in any of the code at the moment while I try to get the pointer working).

Do I have to initialize the player state somewhere?

“I did have the player state set as a derived blueprint but setting it to the class itself did not solve it unfortunately.”

^Do you mean that your BP class derived from your CustomPlayerState was set as the active PlayerState in the GameMode?

Also, what do you mean by IN the Destroy() function? I guess you have binded your own function to OnDestroyed()? I honestly do not know why it wouldn’t work from there, but what if you try it in EndPlay()?

Can you successfully cast to your custom PlayerState from anywhere else?

That is indeed what I meant. I then switched it to simply the C++ class itself, which had no effect. Yes I did bind my own to OnDestroyed() and have had other casts work from that location, but I was simply just wondering.

EDIT: Oddly enough, it has magically started working now. Thanks for all your help everyone! Not sure what the issue was, glad it is working now though :smiley:

IMO that is bad news.

Why do you think that PotemS?

Because you still don’t know what caused it, and things like that tend to start happening again at the worst possible time.

Ah, that makes sense and is oh so true. I do wish I knew what the issue was. It worked after I copied and pasted the cast into the OnTriggerEnter (pointer didnt return null), so then (in same play session) I tried it with the way that used to crash the editor and that worked as well. I then removed it from OnTriggerEnter() and it continued to work in multiple (different) play sessions and continues to work when I quickly tested it today.