Changing gamemode at runtime.

I just wanted to check what was needed to change the gamemode at runtime. I setup a dummy level with all of the current gamemode options desired, and can’t get GetWorld()->SetGamMode(url) to change the current GameMode through C++. Being able to change the GameMode at runtime would vastly simplify having to setup hud management, and controls.

I’m trying to change from the normal combat based GemeMode to one that has settings for navigating through text menus.

My current code for changing the GameMode is:


    FURL url = FURL(NoInit);   
    url.Map = "DialogueUILevel";
    url.Protocol = "unreal";
    if(GetWorld()->SetGameMode(url))
        GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red,
                                         TEXT("DialogueUILevel game mode loaded"));
    else
        GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("game mode not loaded"));




When you say text menus, do you mean something like a pause menu? Or something like a main menu?

In the case of a pause menu, I would just have that be a small chunk of your PlayerController.

In the case of a main menu, I would have that be a separate level with its own game mode. Before you travel to the game level, store whatever information you need onto you GameInstance subclass (or write out to a file / cloud server / whatever if that makes more sense for your game, but it probably won’t).

I’m pretty sure UE4 doesn’t intend you to swap out the game mode mid-game.

UWorld::SetGameMode(FURL url) only spawns a new GameMode if the current GameMode (AuthorityGameMode) is null. Since the map is loaded, it is not null.

When I say text menus, I mean a dialogue window, like you have in Dragon’s Age or Mass Effect. When that pops up the input and HUD literally changes. I’m trying to do that without breaking the game flow, by having to load a separate scene for every dialogue.

I’m not entirely sure, but I would say: You can’t change the GameMode without reloading the Level.

I could be wrong here, but I don’t think UE4 is designed to switch out the GameMode in the middle of the game.
This would also switch out PlayerControllers etc and I doubt that this is easily possible without disconnecting people,
even in Singleplayer.

If you want to change the GameMode, you can do this:

Pre 4.14, you can add lines to your DefaultGame.ini to add shortcuts. This is done similar to this:


[/Script/Engine.GameMode]
+GameModeClassAliases=(ShortName="Group and Chat",GameClassName="/Game/BPNetworkingProject/Blueprints/GroupInviteChat/GICGameMode.GICGameMode_C")
+GameModeClassAliases=(ShortName="Free For All",GameClassName="/Game/BPNetworkingProject/Blueprints/DeathmatchLevel/DMGameMode.DMGameMode_C")
+GameModeClassAliases=(ShortName="Team-based",GameClassName="/Game/BPNetworkingProject/Blueprints/TeamDeathmatchLevel/TDMGameMode.TDMGameMode_C")

In 4.14 you can do this through the Maps and Modes tab in your Project settings.

This allows you to do this is in the OpenLevel URL:


open LEVELNAME?game=GameModeShortcut

After this, the map will be opened with the new GameMode.

1 Like

Yeah, I’d do that by having your HUD wrapped in a blank HUD that handles swapping out inner-HUDs when you context switch.

Here’s a similar example of this: Imgur: The magic of the Internet

It’s a main menu where the wrapper stores a reference to each sub-screen, and swaps out the required one into the container as needed. You could do the same idea, just with your in-game stuff.

Thanks, I’ll look into that.

Now, in a more general direction if I was to change the GameMode so it could be switched at Runtime. Is that something Epic is likely to accept?

I want to make sure this is high in search results. As eXi mentions above, you can select the game mode via the options string with Game Mode Class Aliases set in the Default Modes section of Maps & Modes in Project Settings. Be sure to click the down arrow to expand the section. Different game modes in the same level for UE4. Multiple game modes for the same level.

Although he’s mentioning the OpenLevel URL, this is also possible with the options string in the OpenLevel node, which might be the same thing.

1 Like

I Tried this with 4.23 Engine And There are minor changes I had to done

It was not DefaultGame.ini, It was DefaultEngine.ini

[/Script/EngineSettings.GameMapsSettings]

+GameModeClassAliases=(Name=“TDMMode”,GameMode=/Game/Blueprints/GameModes/BP_TDMGameMode.BP_TDMGameMode_C)

As Xenon2112 mentioned above, Under Maps & Modes Advanced section Aliases can be added.

4 Likes

Just to document this somewhere: The syntax for loading a level with a concrete game mode whose alias is set to “YourGameMode” is

UGameplayStatics::OpenLevel(
    GetWorld(),
    FName("/Game/Path/To/Your/Level"),
    false,
    "game=YourGameMode"
);