8 May

Refactoring AGameMode Part 1 – Why Do That Thing?

First, a little background. Unreal Engine 4 has a pretty pivotal class called AGameMode, which every game has to have.  If you look at the documentation for GameMode, it says that “The AGameMode class defines the game being played, and enforces the game rules.”

I’ve never seen an actual example of that, so I have no idea how it’s ideally supposed to happen. In practice, it does some very important stuff, like starting and restarting players, delegating login to AGameSession, etc.

Unfortunately, it also does some stuff that more advanced game developers won’t want. AGameMode includes a hardcoded state machine, which at certain states does stuff like spawning players, allows network connections, etc. For someone (an engineer) who wants control over when and how that happens, it’s a bit maddening.

It also appears to have been designed with one game type in mind (a multiplayer FPS, perhaps?)

Here’s the possible states:

As you can see just from the names, there’s things there that won’t apply to certain game types. What’s a match? My single player RPG doesn’t have a match. What does EnteringMap mean? No idea, my game doesn’t use the Unreal maps.

Another side effect is that in a couple places in the engine code, it sets the state of the FSM. This kind of thing, with engine code reaching into game code is bad, and we’ll remove it.

Now, this is combined with a hardcoded state machine. This is how you change states in this state machine.

As an engineer who is very fond of my Finite State Machines, this makes me cringe, a lot. It’s very difficult to expunge this hardcoded FSM as well, the easiest way to do that is to split AGameMode (and essentially leave it alone) and make a new base class. That’s what I’ve done, and what this series of articles will document.

These changes are currently in a branch in my own fork of UE4, and I will be contributing them as a pull request at some point.

 Other articles in this series:


One thought on “Refactoring AGameMode Part 1 – Why Do That Thing?

  1. Pingback: In a terrible between-versions limbo in Unreal Engine 4. | Tamed Tornado Software

Leave a Reply

Your email address will not be published. Required fields are marked *