Problems creating variables or pointers in a own GameInstance Subsystem in C++

Hi, I’ve created a GameInstance Subsystem to create an Actor Container (to controll all Actors in my scene, be able to do search between them and pawns, etc.). I created a Game Instance Subsystem C++ class based on UGameInstanceSubsystem. The subsystem seems to create correctly, and can call functions from Actors to the Subsystem (I did a function that just logs out a text, and works properly).

The problem is that in the Subsystem, I cannot create variables, pointers, TArrays… When I create them, the compile process will success (via Live Coding), but when execute the game, the engine crash…

You know what can cause those problems?

Here’s the code:
.h:

#include “CoreMinimal.h”
#include “Subsystems/GameInstanceSubsystem.h”
#include “GeneralContainer_GISubsystem.generated.h”
UCLASS()
class HTN_PLANNIG_UE5_VE_API UGeneralContainer_GISubsystem : public UGameInstanceSubsystem
{
GENERATED_BODY()
public:
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
virtual void Deinitialize() override;
public:
void PrintTest();
// class RandomClass* random; // this will crash the engine
// int a; // this will crash the engine
};

And the .cpp:

#include “Subsystems/GeneralContainer/GeneralContainer_GISubsystem.h”
#include “GeneralProjectContainer/GeneralProjectContainer.h”
void UGeneralContainer_GISubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
UE_LOG(LogTemp, Display, TEXT(“General Container Game Instance Subsystem initialized!”));
}
void UGeneralContainer_GISubsystem::Deinitialize()
{
Super::Deinitialize();
}
void UGeneralContainer_GISubsystem::PrintTest()
{
UE_LOG(LogTemp, Display, TEXT(“General Container Game Instance Subsystem Printed done!”));
}

Both logs are printed.
Note: I didn’t make any modification in the editor, and in any other C++ file.
Note2: I’m using Unreal Engine 5.1

Note after fixing the problem: If I compile from VS with Engine closed without adding any variable, the problem persisted when I created one. But, if I first create the variable, then close Engine, ReBuild from VS, then worked.
At the end I maintained a class independently, and the subsystem just have a pointer to that class.

Thanks!

Hi hunter240,

The first thing to try would be to close UE and compile from Visual Studio and run that - that fixes a lot of issues like this…

1 Like

Use UWorldSubsystem instead:

UWorldSubsystem Base class for auto instanced and initialized systems that share the lifetime of a UWorld

The UGameInstanceSubsystem remains even after map change, so your pointers will be invalid.

UGameInstanceSubsystem Base class for auto instanced and initialized systems that share the lifetime of the game instance

There is also ULevelInstanceSubsystem but I never used it.

Also, use:

UPROPERTY()
TObjectPtr<class RandomClass> Random;

Instead of:

class RandomClass* random;
2 Likes

It sounds like we need an update to the list of available subsystems :smiley:

So, @hunter240 if you’re using UE4, then yes, this is what will happen. UE5 I’ve heard handles changes to class structures in a better fashion, re-loading and re-instancing any objects in the world … but I can see how that might muck things up as well.

So, exit the game, rebuild, and your stuff should appear. Live Coding in UE4 does not handle changes to the structure of a class, and changing structure of a class can blow up the editor.

1 Like

Thanks for your tips! Also helped with the problem! At the end I maintained GameInstance, and it seems to work properly rebuilding from VS. I’m not making a video game, I’m doing a project that only needs one scene, that’s why I choose GameInstance instead of UWorldSubsystem. If I have any problems in the near future I’ll change to UWorldSubsystem!

Thanks!!!