Any Game State good example?

first of all, let me introduces my self: i’m an engineer with and MSc in AI building robotics simulators on my spare time. I’ve started with UE4 a few weeks ago, and everything is going smoothly.

I’ve set up a City Simulator, with Cesium Plugin and so on. I have an array of elements placed during Game Mode Base initialization, without any issue. Also, I’ve a Game Controller to read keyboard inputs to interact with the simulator’s elements.

I would like to access that array of elements from every element in the game (i.e. to change camera views, linked to each element). As far as I understood, the Game State would be the place to store my elements array. I’ve look for any good example to define my own Game State class, without success. Also, I have attached the custom class from the editor, but I cannot access its elements, no matter from where.

My custom Game State class looks like:

class VERTIPORTSIMULATOR_API ASimulatorState : public AGameStateBase
	TArray<AVertiport*> VertiportList;
	TArray<AVertiport*> GetVertiportArray();

I’m getting errors initializing the Game State on the GameModeBase:

GameState = ASimulatorState::StaticClass() //This is not working.

So, here is the question:

  1. What is the proper way of instantiating a custom Game State class?
  2. Is there any good tutorial or example to manage custom Game State?

Thank you very much for your help. Best,

Use GameStateClass not GameState.

Thanks JoSf! You were right. By using GameStateClass, now I can initialize my custom game state.

Now, the problem is that, although GameState seems ok on my GameModeBase, when I try to access it from another class (I tried to obtain a value inside GameState from a APlayerController), the World, and it’s GameState are empty.

In order to access it I’m using UWorld* World = GetWorld() and then, World->GetGameState.

Any idea? Thanks in advanced!

1 Like

Could you show your code please?

Sure Natalo77:

First of all, I’ve the GameModeBase class. I initialize both the GameState and the PlayerController on the constructor:

	PlayerControllerClass = ASimulationController::StaticClass();
	GameStateClass = ASimulatorState::StaticClass();

Secondly, on BeginPlay, I setup my Game and I add the Vertiports objects to the GameState array. This is working just fine:

void AVertiportSimulatorGameModeBase::BeginPlay()
   UWorld* const World = GetWorld();
   // More setup ....
        //Some more conditions....
        //Then I instantiate a Vertiport Object 
        AVertiport* Vertiport = World->SpawnActor<AVertiport>(AVertiport::StaticClass(), FVector(0,0,0), FRotator(0,0,0));
	    Vertiport->Initialize(vertiport.NoOfPads, FVector(vertiport.Longitude,vertiport.Latitude,vertiport.Height), FVector(0,0,0));

Then, in the SimulatorController, I’m trying to access the GameState when a key is pressed:

void ASimulationController::ChangeCamera()
	UWorld* World = GetWorld();
	TArray<AVertiport*> VertiportList = World->GetGameState<ASimulatorState>()->GetVertiportArray();
	for(auto &item : VertiportList)
			UGameplayStatics::GetPlayerController(GetWorld(), 0)->SetViewTarget(item);

Debugging (with a break point just after the Lot(Array)) and evaluating the elements in the array, I get these issues:

Captura de pantalla 2022-02-04 a las 21.04.41

Array elements are 0, while 2 Vertiports were added to the vector in the GameBase. Also, World is not available, which looks worse to me…

Any idea? Thank you very much for your help!


Personally I haven’t tried the multiplayer too much, but try:

TArray<AVertiport*> VertiportList;

Oh and you’d need to add an override for the GetLifetimeReplicatedProps function as well when using Replicated properties.

Though, if you aren’t doing multiplayer, you could just get the GameMode’s gamestate object or not use GameState class at all.

Thank you so much! That was the solution!
Now I can share information between GameState and GameControllers. Great!

1 Like