Subsystems🔗

Subsystems are one of unreal's ways to collect common functionality into easily accessible singletons. See the Unreal Documentation on Programming Subsystems for more details.

Using a Subsystem🔗

Subsystems in script can be retrieved by using USubsystemClass::Get().

void TestCreateNewLevel()
{
    auto LevelEditorSubsystem = ULevelEditorSubsystem::Get();
    LevelEditorSubsystem.NewLevel("/Game/NewLevel");
}

Note: Many subsystems are Editor Subsystems and cannot be used in packaged games.
Make sure you only use editor subsystems inside Editor-Only Script.

Creating a Subsystem🔗

To allow creating subsystems in script, helper base classes are available to inherit from that expose overridable functions.
These are:

For example, a scripted world subsystem might look like this:

class UMyGameWorldSubsystem : UScriptWorldSubsystem
{
    UFUNCTION(BlueprintOverride)
    void Initialize()
    {
        Print("MyGame World Subsystem Initialized!");
    }

    UFUNCTION(BlueprintOverride)
    void Tick(float DeltaTime)
    {
        Print("Tick");
    }

    // Create functions on the subsystem to expose functionality
    UFUNCTION()
    void LookAtMyActor(AActor Actor)
    {
    }
}

void UseMyGameWorldSubsystem()
{
    auto MySubsystem = UMyGameWorldSubsystem::Get();
    MySubsystem.LookAtMyActor(nullptr);
}

Any UFUNCTIONs you've declared can also be accessed from blueprint on your subsystem:

Local Player Subsystems🔗

In case of local player subsystems, you need to pass the which ULocalPlayer to retrieve the subsystem for into the ::Get() function:

class UMyPlayerSubsystem : UScriptLocalPlayerSubsystem
{
}

void UseScriptedPlayerSubsystem()
{
    ULocalPlayer RelevantPlayer = Gameplay::GetPlayerController(0).LocalPlayer;
    auto MySubsystem = UMyPlayerSubsystem::Get(RelevantPlayer);
}

Note: It is also possible to directly pass an APlayerController when retrieving a local player subsystem.