Unreal Engine Tutorial: Setting custom Game Modes at runtime using Blueprints

For the longest time, I believed that it was not possible to change Game Modes at runtime through blueprints. That it had to be specified in the editor either through Project Settings or the World settings for individual maps. Fortunately, I was wrong and there actually does seem to be a method for overriding the default Game Mode through blueprints.

I was recently involved in a project that required setting up completely different types of gameplay based on whether the player chose to play as the attacker or the defender. While it was definitely achievable by creating separate maps for each game mode, it just seemed  redundant given that the maps are identical in all other respects. It made a lot more sense to have two separate game modes with their own player controllers and HUD classes. And after some research along that line of thought, I realized that the Open Level node had more tricks up its sleeves than just opening a new level.

The Open Level node as the name suggests, is what you call when you want to move to a different level. But if you expand the node, you'll come across a new input parameter named Options. This can be used to pass in additional string commands when you open the new level. I'm not sure what exactly that entails in terms of possibilities, but we can definitely use it to override the default Game Mode associated with the level. All you have to do is specify the asset reference path for your Game Mode as per the format shown below:

?Game=GameModeReferencePath_C

You can get the asset path by right-clicking on your Game Mode asset in the Content Browser, and selecting the Copy Reference option. What we need is basically the part within the single quotation marks. For example, this is what I get upon using Copy Reference:
Blueprint'/Game/TowerDefenseStarterKit/Blueprints/GameModes/BP_GameMode_TowerDefense.BP_GameMode_TowerDefense'

So my reference path is:
/Game/TowerDefenseStarterKit/Blueprints/GameModes/BP_GameMode_TowerDefense.BP_GameMode_TowerDefense

With the path obtained, the final string command for this example scenario will be as shown below:
?Game=/Game/TowerDefenseStarterKit/Blueprints/GameModes/BP_GameMode_TowerDefense.BP_GameMode_TowerDefense_C

Now all that's left is to type this in the Options parameter, and next time this Open Level node gets called, it will also make sure that our custom Game Mode overrides the default Game Mode. If that sounds confusing, here is an example of what it looks like in my project (Select node not required; the path can be directly entered in the Options parameter):


And that's all there is to it. This was pretty much completely new information to me, and it saved me the trouble of going about some roundabout fashion to meet the design requirements. And now hopefully, this information will be of help to others as well.

Comments

  1. Can you change game modes upon overlapping a volume?

    ReplyDelete
    Replies
    1. I don't think that's possible unless you open a new similar level upon overlapping.

      Delete
    2. Is it possible to run options like this with listen server and if so how would the listen server be placed?

      listen ?Game=/...
      or
      listen Game=/...
      or something else all together? thanks in advanced!

      Delete
    3. Hi I don't have any experience working with networking in Unreal. Your best bet would be asking in the forums.

      Delete
    4. Just so everyone is aware in future of finding this, I found it out eventually.
      so:
      listen ?Game=/... (Doesn't work)
      ?Game=/...?listen (Doesn't work)
      but
      ?listen?Game=/... (Does Work)

      Delete
  2. This works well in editor for me, but doesn't seem to work in a packaged build.

    ReplyDelete
    Replies
    1. This was because of course, the alternative game mode wasn't being packaged since it wasn't referenced in any map. Forcing to package all game mode assets in the packaging settings fixed it.

      Delete
    2. Actually I didn't really consider that possibility. Thanks for sharing that info. It should be helpful for others as well.

      Delete

Post a Comment