I want to create a subsystem that will do some actors search and store their references for later optimized use.
I’ve made a subsystem subclassed from UWorldSubsystem. But I’m surprised that in my method Initialize(FSubsystemCollectionBase& Collection) the
GetWorld() return a world not yet populated with actors. Is it too early? I also tried to subclass from other subsystem without success.
Example from LyraAudioMixEffectsSubsystem ( Lyra )
LyraAudioMixEffectsSubsystem.h
UCLASS()
class LYRAGAME_API ULyraAudioMixEffectsSubsystem : public UWorldSubsystem
{
GENERATED_BODY()
public:
// USubsystem implementation Begin
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
virtual void Deinitialize() override;
// USubsystem implementation End
virtual bool ShouldCreateSubsystem(UObject* Outer) const override;
/** Called once all UWorldSubsystems have been initialized */
virtual void PostInitialize() override;
/** Called when world is ready to start gameplay before the game mode transitions to the correct state and call BeginPlay on all actors */
virtual void OnWorldBeginPlay(UWorld& InWorld) override;
protected:
// Called when determining whether to create this Subsystem
virtual bool DoesSupportWorldType(const EWorldType::Type WorldType) const override;
...
};
LyraAudioMixEffectsSubsystem.cpp
void ULyraAudioMixEffectsSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
}
void ULyraAudioMixEffectsSubsystem::Deinitialize()
{
//...
Super::Deinitialize();
}
bool ULyraAudioMixEffectsSubsystem::ShouldCreateSubsystem(UObject* Outer) const
{
bool bShouldCreateSubsystem = Super::ShouldCreateSubsystem(Outer);
if (Outer)
{
if (UWorld* World = Outer->GetWorld())
{
bShouldCreateSubsystem = DoesSupportWorldType(World->WorldType) && bShouldCreateSubsystem;
}
}
return bShouldCreateSubsystem;
}
void ULyraAudioMixEffectsSubsystem::PostInitialize()
{
// Load your variables
}
void ULyraAudioMixEffectsSubsystem::OnWorldBeginPlay(UWorld& InWorld)
{
if (const UWorld* World = InWorld.GetWorld())
{
//...
}
}
bool ULyraAudioMixEffectsSubsystem::DoesSupportWorldType(const EWorldType::Type World) const
{
// We only need this subsystem on Game worlds (PIE included)
return (World == EWorldType::Game || World == EWorldType::PIE);
}