Download

How to access GameState from client code?

Here’s how I have things set up:

I have my custom GameState class called UC_GameState. I made a blueprint out of it and assigned it as default GameState class.

Then I try to access it from my custom PlayerController class like this:

GameState = (AUC_GameState*)GetWorld()->GetGameState<AUC_GameState>();
check(GameState);

But the GameState is set to null, and editor crashes.

Shouldn’t GameState be available under client code?

Thanks!

GameState should be available to all clients yeh, they have a local copy and it’s just a big list of replicated variables usually.

ShooterGame get’s the gameState in PlayerController like so:



AShooterGameState* const MyGameState = GetWorld() != NULL ? GetWorld()->GetGameState<AShooterGameState>() : NULL;


So does this mean that it’s possible for the GameState is null? Therefore I can’t rely on retrieving values from this object?

In my example I’m storing some properties of my “characters”. Like HealhtPoints, start rotation etc. Looks like this class isn’t suitable for this? Should I move this properties to GameMode class?

edit: Oh I see, It’s working now. (Because I’m not trying to access it from constructor of PlayerController).

What do you mean by you weren’t trying to access it from the player controller constructor? I’m running into the same issue where the game state is returning null in the client