# ModularGameplayActors

## `Modular Gameplay` Plugin Overview <a href="#modular-gameplay-plugin-overview" id="modular-gameplay-plugin-overview"></a>

A `Modular Gameplay` Plugin is a plugin whose base classes implement the `Modular Gameplay` pattern, which allows your project to inject components into actors at runtime.

This pattern gives the game support for [`GameFeature` Plugins](https://x157.github.io/UE5/GameFeatures/).

The `ModularGameplayActors` Plugin is a specific implementation of `Modular Gameplay`, distributed in GCC.

You can either actually base your classes on the `ModularGameplayActors` classes, or you can implement the same patterns in your own existing base classes.

### Game Framework Component Manager <a href="#game-framework-component-manager" id="game-framework-component-manager"></a>

`UGameFrameworkComponentManager` « `UGameInstanceSubsystem`

This Game Instance Subsystem is what allows GCC to inject components into Actors at runtime.

Actors must explicitly opt-in to this behavior. Epic publishes the `ModularGameplayActors` plugin with `LyraStarterGame` and `ValleyOfTheAncient`demo as an easy set of base classes to automate this opt-in process, but you don’t necessarily have to base your classes on those. You can duplicate the procedure in your own classes if you prefer.

#### Debugging Tip <a href="#debugging-tip" id="debugging-tip"></a>

Execute `Log LogModularGameplay Verbose` in the console to see verbose logging for this module.

Execute `ModularGameplay.DumpGameFrameworkComponentManagers` in the console to dump debugging info to help understand which components are being injected into which actors.

### Game Framework Init State Interface <a href="#game-framework-init-state-interface" id="game-framework-init-state-interface"></a>

`IGameFrameworkInitStateInterface` must be implemented by any Component that needs to initialize based on dependencies that themselves are other Components with their own dependent initialization steps.

TLDR every component in the Game Framework spams `Init()` until everything has successfully initialized or has finally failed initialization.

### Implementation in  Pawn Extension <a href="#implementation-in-lyra-51-pawn-extension" id="implementation-in-lyra-51-pawn-extension"></a>

To implement the `Modular Gameplay` Plugin, take a look at GCCPawnExtensionComponent as an example.&#x20;

* Defines the `UGCCPawnExtensionComponent` that drives `IGameFrameworkInitStateInterface`
  * Adds this component to every Character in the game (via base class `AGCCModularCharacter`)
* Implements `IGameFrameworkInitStateInterface` in other components as needed

#### Pawn Extension Component <a href="#lyra-pawn-extension-component" id="lyra-pawn-extension-component"></a>

`UGCCPawnExtensionComponent` is the implementation of the [`ModularGameplay` Plugin](https://x157.github.io/UE5/ModularGameplay/)’s [`IGameFrameworkInitStateInterface`](https://x157.github.io/UE5/ModularGameplay/#GameFrameworkInitStateInterface) functionality on a Pawn.

Giving this component to an Actor allows the other components on that actor to share Init State updates with each other to satisfy runtime dependencies. The components can hook into `Actor Init State Changed` events broadcast by this component if/when they have runtime dependencies to satisfy.

Note that, though this is a component, there are some deep integrations in `AGCCModularCharacter` for things like, for example, calling `UGCCPawnExtensionComponent`🡒`HandleControllerChanged` from `AGCCModularCharacter`🡒`PossessedBy`. If you want a deep understanding of what this component is doing, make sure you read through `AGCCModularCharacter` as well.

**`OnRegister`**

* Register self with `ModularGameplay` plugin’s `UGameFrameworkComponentManager`
  * Pawn Extension Component implements feature name `PawnExtension`

**`BeginPlay`**

* Bind `Actor Init State Changed` event to `UGCCPawnExtensionComponent`🡒`OnActorInitStateChanged`

**Pawn Extension Component 🡒 `OnActorInitStateChanged`**

* Every time a feature (**NOT** including the `PawnExtension` feature itself) changes state:
  * If new state == `DataAvailable`:
    * Run `CheckDefaultInitialization()` on all feature components

**Pawn Extension Component :: `CheckDefaultInitialization`**

This tells every component on the owner Actor that supports the [`IGameFrameworkInitStateInterface`](https://x157.github.io/UE5/ModularGameplay/#GameFrameworkInitStateInterface) to try to initialize.

This gets spammed A LOT during initialization. This is a trigger that keeps getting executed until all components have initialized successfully or finally fail to initialize.

Make sure to setup your default game instance to `GCCGameInstance` in `Edit->ProjectSettings->Maps And Modes -> Game Instance-> Game Instance Class`

<figure><img src="https://3172028040-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVR559fdf6Z4L9dTMv7RT%2Fuploads%2FVWFZ617h0E04JvGCnsoz%2FScreenshot%202023-12-30%20220938.png?alt=media&#x26;token=33f40374-eacf-420e-9147-3e0fb4bc8618" alt=""><figcaption></figcaption></figure>

## GCCModularCharacter

Minimal class that supports extension by game feature plugins

This class is intended to be used by Player-Controlled actors with AbilitySystemComponent living in the PlayerState

<figure><img src="https://3172028040-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVR559fdf6Z4L9dTMv7RT%2Fuploads%2FSju4qwmdZ2qYV7m5qXDC%2FScreenshot%202023-12-30%20071937.png?alt=media&#x26;token=f39bc4c6-8330-4ceb-aed2-e6aacb31ae1e" alt=""><figcaption></figcaption></figure>

### AbilitySystemComponent and ReplicationMode

Type: `EGameplayEffectReplicationMode`

Enabled: `Mixed`

How gameplay effects will be replicated to clients

| Name    | Functionality                                                                                                                |
| ------- | ---------------------------------------------------------------------------------------------------------------------------- |
| Minimal | Only replicate minimal gameplay effect info. Note: this does not work for Owned AbilitySystemComponents (Use Mixed instead). |
| Mixed   | Only replicate minimal gameplay effect info to simulated proxies but full info to owners and autonomous proxies              |
| Full    | Replicate full gameplay info to all                                                                                          |

## GCCModularPawn

Minimal class that supports extension by game feature plugins

This class is intended to be used by AI-Controlled actors with AbilitySystemComponent living in this class

### AbilitySystemComponent and ReplicationMode <a href="#replicationmode" id="replicationmode"></a>

Type: `EGameplayEffectReplicationMode`

Enabled: `Minimal`

How gameplay effects will be replicated to clients

<table><thead><tr><th width="545">Name</th><th>Functionality</th></tr></thead><tbody><tr><td>Minimal</td><td>Only replicate minimal gameplay effect info. Note: this does not work for Owned AbilitySystemComponents (Use Mixed instead).</td></tr><tr><td>Mixed</td><td>Only replicate minimal gameplay effect info to simulated proxies but full info to owners and autonomous proxies</td></tr><tr><td>Full</td><td>Replicate full gameplay info to all</td></tr></tbody></table>

## GCCModularPlayerController

Minimal class that supports extension by game feature plugins

This class is meant to be use with [`UGCCModularCharacter`](#gccmodularcharacter)

## GCCModularAIController

Minimal class that supports extension by game feature plugins

This class is intended to use with `UGCCModularPawn` class to build AI-Controlled characters

## GCCModularGameplayActor

Actor class that implement lazy loading for actor type like World Actor

## Introduction <a href="#introduction" id="introduction"></a>

The Ability System Component (ASC) can be a bit resource-intensive, especially when dealing with World Actors that require it, like Damageables. If our world is populated with such Actors, their memory footprint can become noticeable.

Taking a cue from Epic and the insights shared by Tranek in the excellent [GASDocumentation](https://github.com/tranek/GASDocumentation#optimizations-asclazyloading), Fortnite adopts a smart approach. They lazily load their ASCs in their World Actors just when they’re needed, dodging the memory hit until it’s absolutely necessary. This memory optimization is a significant boost for scalability; since ASC World Actors won’t carry that footprint unless explicitly interacted with.

Think of it like this: buildings, trees, and damageable props on standby with a lower cost, only racking up the memory bill when they step into the gameplay spotlight.

([`Vorixo Lazy Loading Blog`](https://vorixo.github.io/devtricks/lazy-loading-asc/))

### AbilitySystemComponent and ReplicationMode <a href="#replicationmode" id="replicationmode"></a>

Type: `EGameplayEffectReplicationMode`

Enabled: `Minimal`

How gameplay effects will be replicated to clients

<table><thead><tr><th width="545">Name</th><th>Functionality</th></tr></thead><tbody><tr><td>Minimal</td><td>Only replicate minimal gameplay effect info. Note: this does not work for Owned AbilitySystemComponents (Use Mixed instead).</td></tr><tr><td>Mixed</td><td>Only replicate minimal gameplay effect info to simulated proxies but full info to owners and autonomous proxies</td></tr><tr><td>Full</td><td>Replicate full gameplay info to all</td></tr></tbody></table>

### Class Default Objects (CDO)

<figure><img src="https://3172028040-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVR559fdf6Z4L9dTMv7RT%2Fuploads%2FI97gqyM8rzz58idR8A0k%2FScreenshot%202023-12-30%20221309.png?alt=media&#x26;token=d6acdfa6-2e32-41f9-9d5e-a7772ad52aec" alt=""><figcaption></figcaption></figure>

* `AbilitySystemComponent class` - AbilitySystemComponent to be use by this actor
* `AbilitySystemComponent Creation Policy` - Defines how a the Ability System is loaded
* `Attribute Sets` - Attribute Set to be use by this actor

## GCCModularPlayerState

Minimal class that supports extension by game feature plugins

This class is meant to be use with `UGCCModularCharacter` with ASC living in PlayerState

### AbilitySystemComponent and ReplicationMode <a href="#replicationmode" id="replicationmode"></a>

Type: `EGameplayEffectReplicationMode`

Enabled : `Mixed`

How gameplay effects will be replicated to clients

| Name    | Functionality                                                                                                                |
| ------- | ---------------------------------------------------------------------------------------------------------------------------- |
| Minimal | Only replicate minimal gameplay effect info. Note: this does not work for Owned AbilitySystemComponents (Use Mixed instead). |
| Mixed   | Only replicate minimal gameplay effect info to simulated proxies but full info to owners and autonomous proxies              |
| Full    | Replicate full gameplay info to all                                                                                          |
