Skip to content

Instantly share code, notes, and snippets.

@v1vendi
Created September 16, 2022 13:00
Show Gist options
  • Save v1vendi/871c0859e238567eaa39c6a5656ee178 to your computer and use it in GitHub Desktop.
Save v1vendi/871c0859e238567eaa39c6a5656ee178 to your computer and use it in GitHub Desktop.
Unreal Engine Subsystem with separate logic and configurable blueprints in settings
#include "MySubsystem.h"
#include "Engine/LocalPlayer.h"
#include "Blueprint/UserWidget.h"
UMyLogic::UMyLogic()
{
UE_LOG(LogTemp, Log, TEXT("Logic created"));
}
void UMyLogic::CreateMyWidget(ULocalPlayer* LP)
{
const auto Settings = GetDefault<UMyDeveloperSettings>();
TSubclassOf<UUserWidget> WidgetClass = Settings->MyWidgetClassFromSettings.LoadSynchronous();
MyWidget = CreateWidget<UUserWidget>(LP->GetWorld(), WidgetClass);
MyWidget->AddToViewport();
UE_LOG(LogTemp, Log, TEXT("Logic::CreateWidget called"));
}
UMySubsystem::UMySubsystem()
{
MyLogic = CreateDefaultSubobject<UMyLogic>(TEXT("MyLogic"));
UE_LOG(LogTemp, Log, TEXT("Subsystem created"));
}
void UMySubsystem::OpenWindow()
{
UE_LOG(LogTemp, Log, TEXT("Subsystem::OpenWindow called"));
MyLogic->CreateMyWidget(GetLocalPlayer());
}
#pragma once
#include "Subsystems/LocalPlayerSubsystem.h"
#include "Engine/DeveloperSettings.h"
#include "MySubsystem.generated.h"
class ULocalPlayer;
UCLASS(Config=Game, defaultconfig)
class SUBSYSTEMTEST_API UMyDeveloperSettings : public UDeveloperSettings
{
GENERATED_BODY()
public:
UPROPERTY(Config, EditAnywhere)
TSoftClassPtr<UUserWidget> MyWidgetClassFromSettings;
};
UCLASS()
class UMyLogic : public UObject {
GENERATED_BODY()
public:
UMyLogic();
void CreateMyWidget(ULocalPlayer* LP);
UPROPERTY()
UUserWidget* MyWidget;
};
UCLASS()
class UMySubsystem : public ULocalPlayerSubsystem
{
GENERATED_BODY()
public:
UMySubsystem();
UMyLogic* MyLogic;
UFUNCTION(BlueprintCallable)
void OpenWindow();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment