How to Get AShooterPlayerState From Controller

I am working inside of the ShooterCharacter.cpp in the ShooterDemo project. I am trying to get the player’s team number inside of the SpawnDefualtInventory method. My idea was to get the AShooterPlayerState and then call GetTeamNum(). Unfortunately when trying to cast from a PlayerState to AShooterPlayerState causes the game/editor to crash. The error message I get is:

Access violation - code c0000005 (first/second chance not available)

Here is the code that I’m using to try to achieve my end result. The first line is confirmed to be working correctly. It is the second line that causes the game/editor to crash. Any help on this would be greatly appreciated.

`AShooterPlayerController* MyPC = Cast(Controller);

AShooterPlayerState* PlayerState = Cast(MyPC->PlayerState);
`

First you should not use the PlayerController from your character, it will be NULL on not owning clients, your cast is also a bit odd. You can access the state directly from your Character:

AShooterPlayerState* MyPlayerState = Cast<AShooterPlayerState>(PlayerState);

Thanks for the answer. Unless I’m doing something wrong MyPlayerState is also null when I am calling it inside the SpawnDefualtInventory method.

Since I posted this question I went ahead and overrode InitPlayerState by placing the method inside of the ShooterPlayerController class. Unfortunately I cant call any method from the ShooterCharacter class since it is NULL as well.This is the code I’m using to get the ShooterCharacter:

AShooterCharacter* ShooterCharacter = Cast<AShooterCharacter>(Controller->GetCharacter());

Any help would be greatly appreciated in being able to get the player’s team number inside of SpawnDefualtInventory.

From where are you calling this?

AShooterCharacter* ShooterCharacter = Cast(Controller->GetCharacter());

Is being called from inside of the InitPlayerState method inside of the ShooterPlayerController class. As for the code you posted above, it is being called from inside of the SpawnDefaultInventory method in the ShooterCharacter class. The SpawnDefaultInventory method is being called from the PostInitializeComponets() (Also inside of the ShooterCharacter class).

Ok, Controller is NULL in that state to catch when a PlayerController posses a Pawn (a Character is an inherited Pawn class) override the following method in your PlayerController:

/** Setter for Pawn. Normally should only be used internally when possessing/unpossessing a Pawn. */
virtual void SetPawn(APawn* InPawn) override;

There is also a method when the PlayerController get’s it’s PlayerState, let me search it in a sec ^^:

/** spawns and initializes the PlayerState for this Controller */
virtual void InitPlayerState();

Again, this is in the Controller so it will get only executed for Owning players.

Now I’m a bit more confused and don’t really follow. What I’m supposed to do with the SetPawn method that I have overrode? I guess it might be easier to start from the beginning. I need to be able to get the player’s team number in the SpawnDefaultInv on the server. To do this it I need to access the player state and call GetTeamNum().

The thing is the order you are using, from SpawnDefaultInventory your character class does not have a PlayerController nor a PlayerState, it is too early to access those in PostInitializeComponets. So where should you then do your initialization logic? You got two places that should be save for you: InitPlayerState and SetPawn, the first gets called and the PlayerController gets a PlayerState and the later when it gets a pawn. I would add all team logic in InitPlayerState.

Ok, so basically I was on the right track using InitPlayerState. Looking at the code that was created by the Unreal team. (since Im using the shooter demo). It looks if I move SpawnDefaultInventory to either InitPlayerState and SetPawn I will need both the PlayerState and access to the Shooter Character class. Is this possible? It looks like if I work through the SetPawn method, I could use GetController() and then from that use the GetCharacter() to get the character class I need.

You do not need to call GetController in SetPawn, that method is within your PlayerController already, just cast the pawn that is passed as an argument, that’s the Character you will be using.

Works like a charm. You are a champ! Thank you so much for helping me figure this in one out.