Game instance subsystem not initialized during GameInstance Event Init

Hey,

I’ve been trying to write a plugin that handles communication with a server. It does so using a GameInstance subsystem. I got everything working, but then I started wondering if I could make the most used function static, so I don’t always have to include the Subsystem node when calling it in blueprints.

To get access to my subsystem’s variables when calling the static function, I call GetSubsystem() and this works. except it throws an error when calling it in the Event Init of the Game Instance. The GetSubsystem call returns null here causing the code to fail.

According to documentation, Game Instance subsystems are initialized as the Game Instance itself is initialized but I would expect this to be done before calling the blueprint’s Event Init.

// ...
UWorld* world = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::Assert);
	UGameInstance* instance = world->GetGameInstance();

	UIASSubsystem* IASSubsystem = instance->GetSubsystem<UIASSubsystem>(); // Returns null
// ...

My question is whether it’s normal practice to try and get my variables in this way (I originally tried using static variables but I was getting Unresolved External Symbol errors when I did that) or how else I should go about using the subsystem’s variables in the static method. And if there is a way to force the initialization of the subsystem before the Game Instance’s Event Init runs.

Thanks in advance!

1 Like

I was also bit by this, and also found it weird that the GameInstance Event Init blueprint event was fired before subsystems were initialized and available. The solution I went with was to inherit my own class from GameInstance, override the Init, call Super::Init and then raise my own custom PostSubsystemInit event to blueprints for designers.

UCLASS(Blueprintable, Abstract)
class AWESOME_API UAwesomeGameInstanceBase : public UGameInstance 
{
	GENERATED_BODY()

public:
	virtual void Init() override;

protected:
	/** Opportunity for blueprints to handle the game instance being initialized after the subsystems have been initiatlized. */
	UFUNCTION(BlueprintImplementableEvent, meta=(DisplayName = "PostSubsystemInit"))
	void ReceiveInitPostSubsystem();
};
void UAwesomeGameInstanceBase::Init()
{
	Super::Init();
	ReceiveInitPostSubsystem();
}

In case anyone else also gets bit. It is a tad weird, considering that the official MVVM documentation suggests that the game instance is a convenient place to initialize viewmodels. Yet, the MVVMGameSubsystem isn’t available until after the Game Instance’s init event has fired.

4 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.