How to Recreate a GameInstanceSubsystem After GC

i have create a Gameinstancesubsystem named DevSettingSubsystem,and call this function to make it garbage collection:

UDevSettingSubsystem* Router = UGameInstance::GetSubsystem(GetGameInstance());
if(Router!=nullptr)
{
Router->MarkPendingKill();
}

if i need to use it againe ,how can i recreate this subsystem.because it first initialize was run in GameInstance when game start.

a) You should probably implement Initialize and Deinitialize for you subsystem instead of calling MarkPendingKill()

b) You would start up your subsystem with Initialize

There is one problem though. You can’t really call it manually.
GameInstance has a private struct variable

FObjectSubsystemCollection<UGameInstanceSubsystem> SubsystemCollection;

that holds the subsystems.

It is needed during the calling of Initialize.
The correct override would be

void Initialize(FSubsystemCollectionBase& Collection) override

The problem is you don’t have access to UGameInstance’s SubsystemCollection and there is not getter that returns it.

So the lifecycle seems bound to the internal workings of the UGameInstance class.

  • During void UGameInstance::Init() where the subsystem initialization is called SubsystemCollection.Initialize(this);
  • During void UGameInstance::Shutdown() where SubsystemCollection.Deinitialize(); is called

thanks ,but epic offers subsystem’s lifecycle is bound to gameinstance or localplayer,World,Editor,Engine.those all not easy to destroy when play.if i only need a subsystem when game start and i do not want it stay in ram all the time by gameinstance.just want to find a way to destroy and recreate it when i need it anytime.

You could consider:
a) Adding a custom function to flush / empty variables to clear the memory within the subsystem and just keep it’s shell (should be lightweight).

b) if you are not linking the subsystem to any of the offered parent classes lifecycle then migrate it to an actor that has a normal lifecycle within the world. You can spawn it / destroy it when needed and all is kept clean.

Hi there @lucakd, hope you’re well!

This topic has been moved from International to Programming & Scripting: C++.

When posting, please review the categories to ensure your topic is posted in the most relevant space.

Thanks and happy developing!

keep it shell is a good thinking! i will try it