Retrieve GameMode and/or GameState in Editor

I’m just trying to retrieve the Game Mode and Game State of the EditorWorld. For some reason it is always returning null.

I have made blueprint instances of my c++ classes for both the AGameModeBase and the AGameStateBase. Then in my Project Settings, I use those instances in maps and modes->Default Game Mode and maps and modes->Game State Class.
Then in my UActorComponent, I try to get a handle on these instances like so:

auto EditorWorld = GEditor->GetEditorWorldContext().World();
if (EditorWorld) {
	auto GameState = EditorWorld->GetGameState();
	if (GameState) {
		BMoveSetupComponent = Cast<UBMoveSetupComponent>(GameState->GetComponentByClass(UBMoveSetupComponent::StaticClass()));
	}

	auto GameMode = EditorWorld->GetAuthGameMode();
	if (GameMode) {
		BMoveSetupComponent = Cast<UBMoveSetupComponent>(GameState->GetComponentByClass(UBMoveSetupComponent::StaticClass()));
	}

}

if (BMoveSetupComponent) {
	BMoveSetupComponent->TestFunction();
}

However GameState and GameMode are always NULL? I am not sure what to do?
This is run while the editor is paused because I enabled the UActorComponent to tick while pause. I’d like to be able to retrieve either of these instances at any time. Is this possible?

1 Like

hey I am facing the same problem right now. Did you manage to get the Game mode in the editor world?

Have you tried accessing the world from the viewport?

UWorld* world = GEngine->GameViewport->GetWorld();

Not sure is gamemode is accessible before the game actually begins.