Modular Game Features: What You Need To Know

Modular Game Features: What You Need To Know

Created
Sep 15, 2021 7:50 AM
Tags
UE5C++Blueprints

Overview

The Modular Game Feature system is a new addition to Unreal Engine 4.27 and Unreal Engine 5 that allow developers to author standalone features for their projects.

These standalone features help compartmentalize Unreal projects, thus keeping the codebase clean and readable. And due to the nature of a modular game feature, these will help avoid accidental dependencies from occurring.

In essence, the important aspects of this feature are —

  • Keeps the project codebase clean and readable
  • Helps avoid accidental dependencies from occuring
  • Dynamically loadable and unloadable

In this article, I will be going over what you need to know in order to get up and running with the Modular Game Feature system in Unreal Engine.

icon

If you end up finding this article useful — spread the word!

Requirements

The requirements to utilize the Modular Game Feature system —

Engine Version: 4.27+

Required Plugins:

  • Game Features
  • Modular Gameplay

Important Notes

Here are important aspects to know when you're implementing a new Game Feature —

  • Do not reference Game Features via the base game
  • Game Features should be self-contained
  • Games Features can require other plugins as dependencies if needed
  • Game Features should be loadable and un-loadable without issue
  • If you are cross referencing between Game Features, consider moving those to the base game
  • Game Features can contain code, the default plugin template is just a content only one
  • Game Features have to be placed under /<ProjectName>/Plugins/GameFeatures/*
  • Game Features can be found in the Plugins Window -> Project
  • Game Features will activate for everyone within a multiplayer session
  • Game Features can be done via Blueprints and/or C++
  • Be wary of over utilizing Game Features

C++ Notes

Important notes that are C++ specific

  • GameFeatures must be included in the Build.cs file for the project or plugin
using UnrealBuildTool;

public class UDR_ModularGameplay : ModuleRules
{
	public UDR_ModularGameplay(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "GameFeatures" });

		PrivateDependencyModuleNames.AddRange(new string[] {  });
		
	}
}
  • #include "GameFeaturesSubsystem.h" is the required include

Creating a Game Feature Plugin

1. Enable the Required Plugins

Before you can create a Game Feature, you must enable the following two required plugins —

  • Game Features
  • Modular Gameplay

Once these two plugins have been enabled, restart the editor.

2. Add entry to PrimaryAssetTypesToScan

Once the editor has been restarted, you will receive a message in your message log stating the following —

Asset Manager settings do not include an entry for assets of type GameFeatureData, which is required for game features plugins to function. [Add entry to PrimaryAssetTypesToScan]

To fix this issue follow either the automated or manual method below.

Automated Method

Simply click the link appended to the end of the message — Add entry to PrimaryAssetTypesToScan — to automatically fix that issue.

Manual Method
  1. Navigate to — Project Settings -> Asset Manager
  2. Create a new Primary Asset Types to Scan Array Element
  3. Fill in the following 2 values —
    1. Primary Asset Type: GameFeatureData
    2. Asset Base Class: GameFeatureData
  4. Create a new Directory
  5. Add /Game/Unused In the newly created directory
  6. Everything is all set!

image

Explanation

Adding this entry is required for the engine to acknowledge the Primary Asset Type associated with the Game Modules.

icon

If this message doesn't appear in your Message Log, you can skip this step

3. Creating the Plugin

Creating a game feature plugin is done the same way you create other plugins. To create a new Game Feature plugin, simply follow these steps —

  1. Open the Plugins window — Edit → Plugins
  2. In the bottom right of the window, press the New Plugin button
    1. This will open a New Plugin window
  3. In the New Plugin window —
    1. Select Game Feature (Content Only) from the template list
    2. Enter a plugin name
    3. Enter descriptor data
  4. Once all of the information has been added, press the Create Plugin button in the lower right of the window
  5. The new Game Feature has now been added to your project and a configuration window will appear.
  6. You're all set to start implementing your own Modular Game Features!

icon

The newly created Game Feature can be found in your projects plugins directory — */ProjectDir/Plugins/GameFeatures/*

Opening the Game Feature configuration window

The Game Feature configuration window automatically opens after it has been created. To find and open the configuration window manually, simply —

  1. Open the content browser
    1. Key-bind: Ctrl + A
    2. Window → Content Browser → Content Browser (1, 2, 3, or 4)
  2. Enable Open the Source Panels
    1. Content Browser → Settings → Show Sources Panel
  3. Enable Show Plugin Content
    1. Content Browser → Settings → Show Plugin Content
  4. Navigate to the newly created Game Feature directory in your Content Browsers Source Panel
  5. Open the Game Feature Data Asset that has the same name as the new Game Feature
  6. The configuration window for the newly created Game Feature is now open!

Configurating the Game Feature

With the Game Feature configuration window open, you will find three primary categories — Feature State, Actions, and Game Feature. Each of these play an important role in setting up and defining the game feature itself.

Feature State

The Feature State category controls the initial and current state of the Game Feature. The state of a Game Feature is divided into four options — Installed, Registered, Loaded, and Active.

Initial State

The state of the Game Feature when initially loaded in Editor or Runtime.

Current State

The state the Game Feature currently is in. This can be adjusted at any time.

State — Installed

The Game Feature isn't registered with the engine, and will not be visible in the Content Browser.

State — Registered

The Game Feature is registered with the engine, but not loaded into memory. Note: The ideal Initial State

State — Loaded

The Game Feature is loaded into memory and awaiting activation.

State — Active

The Game Feature is loaded into memory and currently active. Note: Avoid editing the Game Feature while it's set to active.

Actions

The Actions category is where to define what actions to occur when the Game Feature is enabled or disabled.

Here are the actions that are currently available as of Unreal Engine 5 Early Access 2 —

Add Cheats

The Add Cheats action allows developers to easily add their own debug commands via registering a Cheat Manager Extension with the game.

Cheat are purely for debugging and will not be included in shipping builds.

Add Components

The Add Components action will add a component to the designated actor class if it has been registered to receive components via the Game Framework Component Manager.

There are for options within the action —

Actor Class

The actor to attach the component to.

Component Class

The component to add to the actor.

Client Component

Adds the component to clients within a multiplayer session. Enabled by default

Server Component

Adds the components to the server within a multiplayer session Enabled by default

image

Adding an actor as a Receiver

By default, actors have to be added as a receiver to receive Game Feature Components. This step is generally done via BeginPlay.

Depending on the component and needs, consider utilizing C++ to add the receiver during an earlier phase.

Blueprint
image
C++
if (UGameFrameworkComponentManager* ComponentManager = GetGameInstance()->GetSubsystem<UGameFrameworkComponentManager>())
{
    ComponentManager->AddReceiver(this);
}

Removing an actor as a Receiver

It is ideal to cleanup and remove the actor as being a Game Feature Component receiver during the Destroyed or EndPlay events. Note: When an actor is removed as being a receiver, it will automatically remove the Game Feature Components that were added. There is no need to do that manually.

Blueprint
image
C++
if (UGameFrameworkComponentManager* ComponentManager = GetGameInstance()->GetSubsystem<UGameFrameworkComponentManager>())
{
    ComponentManager->RemoveReceiver(this);
}

Add Data Registry

The Add Data Registry Action will add a Data Registry to your project.

Add Data Registry Source

The Add Data Registry Source will add a data source that will stream into a Data Registry.

  • Each configure path to each data source must be configured

Custom Actions

An example of what other actions can be added manually via C++. Some, if not all, will be available when Unreal Engine 5 fully releases.

You can find these in the Valley of the Ancient demo.

  • Add Abilities
  • Add Attributes Defaults
  • Add Input Mapping
  • Add Level Instances
  • Add Spawned Actors
  • Add World Systems

Asset Manager

The Asset Manager category is utilized to tell the engine that new assets are available. For example, introducing new weapons or items to your game.

Setting a Game Feature state during runtime

Game Features can be dynamically enabled and disable during a runtime session.

Blueprint

As of Unreal Engine 5 Early Access 2, there are no native blueprint nodes that allow you to toggle on or off Game Features. Thankfully, Epic has exposed that functionality through console commends. Simply utilize the following two commands with the Execute Console Command blueprint node.

Deactivate a Game Feature

DeactivateGameFeaturePlugin <GameFeatureName>

image

Load & Activate the Game Feature

LoadGameFeaturePlugin <GameFeatureName>

image

Unload the Game Feature

UnLoadGameFeaturePlugin <GameFeatureName>

image

C++

// Get the Plugin URL based on the Game Features Name
FString PluginURL;
UGameFeaturesSubsystem::Get().GetPluginURLForBuiltInPluginByName(<GameFeatureName>, PluginURL);

// Deactivate the Game Feature
UGameFeaturesSubsystem::Get().DeactivateGameFeaturePlugin(PluginURL);

// Activate the Game Feature
UGameFeaturesSubsystem::Get().LoadAndActivateGameFeaturePlugin(PluginURL, FGameFeaturePluginLoadComplete());

// Unload the Game Feature
UGameFeaturesSubsystem::Get().UnloadGameFeaturePlugin(PluginURL);

// Load the Game Feature
UGameFeaturesSubsystem::Get().LoadGameFeaturePlugin(PluginURL, FGameFeaturePluginLoadComplete());

Example

For this example, I have a Coin Collection Game Feature. When the green button is stepped on, the feature is toggle on. When the red button is stepped on, the feature is cleaned up and toggled off.

The Coin Collection Feature spawns collectable coins randomly and attaches a coin counter to the players viewport.

Research Project

You can download, look through, and test the research project here —

Frequently Asked Questions

A list of frequently asked questions regarding this feature, and their answers.

When should I use this feature?

MFG's should be utilized when you need to update or create new features over the life of your project without the project specifically depending on them.

What are some usage examples?

With Game Features not being referenced by the core gameplay, they can be utilized to control aspects of gameplay, memory management, and more on the fly.

For example, the Fortnite dev team utilizes Game Features for their vehicles. This allows them to easily enable or disable them for the different game modes.

Here are some other example use cases —

  • Weapon Unlocks
  • Ability Unlocks
  • Level Instances
  • Items
  • NPC's
  • FX
  • Dialog
  • Weather Effects
  • Cutscenes

Just remember that Game Features should be self contained and disabling them should not affect the core game.

My game feature isn't activating, why is that?

Make sure that you set your Game Feature state to Active when you want to enable it. Likewise, make sure to set it to Loaded or Registered to disable it.

If it still doesn't activate, check your logs. There most likely is a warning or error that will help you pin-point the issue.

Why is it recommended to not have the core game depend on a Game Feature?

When you take away something the engine is expecting to be there, it can cause a whole host of issues, such as —

  • Game Crashes
  • Errors
  • Warnings
  • General bugs

Additional Resources

If you're interested in learning more about this feature, check out the resources below —

icon

Thank you for reading!

My goal with Unreal Directive is to provide an extremely useful and easy to understand resource for Unreal Engine developers across all skill levels. Therefore, if you have found this article useful, spread the word! Please share it with your developer friends and peers. If you have a suggestion, or have come across an issue with the article, please message me via Twitter or via email.

Thank you,