Set GameState from GameMode

By default you can do:

#include "MyGameStateBase.h"

AMyGameModeBase::AMyGameModeBase( )
{
    GameStateClass = AMyGameStateBase::StaticClass( );
}

What if i want to create GameState in runtime and then set it?

Will this solution work?

void AMyGameModeBase::SwapGameState(AGameStateBase* GameState)
{
	GameStateClass = GameState->StaticClass();
}

Hello there.

I have a question about UWorld::SetGameState function.

My goal is to set GameState from GameMode, however I’m no sure how can i do this. Simple calling this like:

void SwapGameState(AGameStateBase * GameStateVar)
{
 UWorld::SetGameState(GameStateVar);
}

doesn’t seem to work or I just can’t provide GameStateVar in proper way. Anyone here can help me?

Good explaining would be appreciated.

GameStateClass* GameState = Cast(this->GetWorld()->GetGameState());

if (!GameState)
{
	UE_LOG(LogTemp, Error, TEXT("FAILED: GameState failed to set in %s!"), *this->GetName());
}

I’m also not sure if you can change your GameState during run-time. What are you trying to do? There might be a better solution.