How can I get a character from a playerstate?

I managed to pull this off by setting it up in the GameMode after PostLogin. I had to add a delay in order to get PlayerState and Controlling pawn without getting None objects back (or perhaps it was just for PlayerState, I can’t remember).

After casting to FirstPersonCharacter and setting the collision profile, the client was unaware of the change. I also added a CollisionProfile rep notify variable onto my FirstPersonCharacter, which I set in the game mode after updating the collision profile. Then, in the notified function, I set it on the client too. Now it all seems to work.

This feels about as hacky as it can get (and I haven’t even mentioned the modulusing I’m doing to try to assign a player to a collision profile). I would still like to hear of better alternatives.

I’m using a custom PlayerState. When a PlayerState is created, I’d like to find the associated character and update its collision profile. Essentially I’m trying to set a different collision profile for each player in the game.

I’m using the first person starter content as a starter point. I’ve tried doing PlayerState->GetOwner->Cast To FirstPersonCharacter, but this fails. I see a GetPawn function here, but it doesn’t appear to be exposed in blueprints. Is there another way to do this? Is there a better way? I had little success getting it to work in the FirstPersonCharacter event graph, even after monkeying around with Switch Has Authority, but I may have done something wrong.

I know it’s late, but in case you get here from google search:
The reason your cast is failing is because the owner of the PlayerState is a PlayerController.
On server, you could get the character like this:
PlayerState->GetOwner->Cast to PlayerController->GetCharacter

It won’t work on clients, because clients dont have other players’ PlayerController. I have no idea how to find there. I guess you could iterate all characters, and check if playerstate is the same

it’s even easier. There is a variable called PawnPrivate in the PlayerState, you can use that from BP (cast it to Character), or in CPP, there is a GetPawn() function.

3 Likes