Get GameMode

I have looked everywhere trying to figure this out, along with playing with code trying to get it to work. Might be really simple and i am just looking too much into it. My goal is to call a function from my game mode in my HUD class ie. trying to figure out how to change HUDs while game is running.

Question:
“How do you get the current GameMode and set it to a pointer so i can call my function?”

Note: I am calling this from a custom SCompoundWidget class. Goal is to call my function in my game mode from a button click and it will set the HUDClass variable of GameMode to a different HUD class.

Use Kismet library UGameplayStatics::GetGameMode | Unreal Engine Documentation and Cast to get a pointer.

Well that seems to create the pointer I needed, but now I have another issue. For some odd reason the game mode won’t update the class and display it.

In my custom SCompoundWidget class i have this as my button click function:

FReply SMainMenuUI::PlayGameClicked()
{
	MenuHUD->PlayGameClicked();
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("PlayGameClicked"));
	TObjectIterator< APlayerController > ThePC;
	UWorld* TheWorld = ThePC->GetWorld();
	ATarragonGameMode* MyGM = Cast<ATarragonGameMode>(UGameplayStatics::GetGameMode(TheWorld));
	MyGM->SetHUD("Login");
	return FReply::Handled();
}

Then in my GameMode i have this function:

void ATarragonGameMode::SetHUD(FString MenuName)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, MenuName);

	if (MenuName == "MainMenu")
		HUDClass = AMainMenuHUD::StaticClass();
	else if (MenuName == "Login")
		HUDClass = ALoginHUD::StaticClass();

}

When i click the button on my MainMenuHUD which the PlayGameClicked function is linked to. It displays the text “PlayGameClicked” and “Login” so it goes into the correct functions, but HUDClass won’t update and show my LoginHUD.

Any Suggestions?

I don’t think you can change default HUD class during runtime (I may be wrong, devs will correct) and more appropriate way to change your current hud class for player is to use APlayerController::ClientSetHUD() function.