GameInstance constructed two times (android)

I think its a bug … the GameInstance is being constructed two times instead of once. And its stated in several places that it should behave like a Singleton …

I have also posted this on forum thread:

Please, note this only happens on Android Devices. Not on PIE.

engine version: 4.6.0

Hey -

Are you using the source code version of the engine from GitHub to package your project or the binary version from the launcher? If you’re using the launcher, does the same behavior occur if using 4.6.1?

Cheers

Hi,

I’m using an engine from GitHub based on 4.6.0 release (commit a56889f061d0d897f4bc3f1736afb27a0bf10358)

I do not have 4.6.1 currently so I can not test it.

So in your device the GameInstance is constructed only once with 4.6.1?

You did not reproduce it in 4.6.0 ?

With a simple Log I see the GameInstance is constructed twice see this copy&paste from my code:

USTPGameInstance::USTPGameInstance(const class FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
	, bMusicEnabled{true}
	, bSoundsEnabled{ true }
	, bDebugProfileEnabled{false}
{
	InventoryDB = ObjectInitializer.CreateDefaultSubobject<UInventoryDB>(this, TEXT("IventoryDB"));

	if (!HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject))
	{
		UE_LOG(LogTemp, Log, TEXT("Initializing InventoryDB from GameInstance path '%s'"), *GetPathName());
		InventoryDB->Initialize();
	}
}

I am having difficulties reproducing the game instance being created twice when trying to package 4.6.0 for an Android devices. If you get the chance try packing in 4.6.1 and let us know if you still see the same behavior.

Hi ,

We have not heard back from you in a few days, so we are marking this post as Resolved for tracking purposes. If you are still experiencing the issue you reported, please respond to this message with additional information and we will offer further assistance.

Thank you.

I am having the same issue. It creates two when running a dedicated server. The only issue with this is that it is impossible to tell when it’s server side or not. I have some custom network stuff that needs to persist across maps and modes but I can’t figure how how to reliably get the role.

A game instance is created for every client and on the server(if dedicated). I do not know if it is possible replicate it as it is not an actor. But if you have things that need to be setup once on the server, such as a FTcpListener, you can create a function and call it from GameMode using a ROLE == ROLE_Authority check and then setting an IsInitialized boolean flag to ensure that you do not call it again. Be sure to check out the First Person Shooter example that is available in the launcher under the learn tab!

Got it! [3 more characters needed…]

Actually my issue has nothing involved with replication because my game is standalone.

Under the play drop down menu, do you have “Run Dedicated Server” checked? That’s about the only thing that I can think of. If not, can you post all code referencing your game instance?

I test this on an android device directly, so there is no Run Dedicated Server possibility.

My game instance is referred by several parts of my code but none of them construct it neither are involved in it construction.

It is constructed at some point in engine initialization and might be related only to android devices.

I’ll see if I can reproduce. I’ll get back to you.

Are you still using 4.6.0 release (commit a56889f061d0d897f4bc3f1736afb27a0bf10358) or using 4.6.1?

It seems to be working normal for 4.6.1. However, the constructor [YourGameInstance::YourGameInstance(const class FPostConstructInitializeProperties& PCIP)] is called when when the editor starts up. this is because the engine needs to create a default object. But GameInstance does have a Init() function that you can override that should only be called when the actual game starts up.

You didn’t supply much information. As a result, it makes it extremely difficult to troubleshoot. Regardless, I feel fairly confident that this is the issue you are having.

I really appreciate your efforts MJLaukala’.

Why do you keep trying on PC?

I said this happens on android. There is no editor startup involved because there is not editor at all.

Hey -

MJLaukala’ does have a valid point of needing to understand how your game instance is created. Are you creating it in code or with blueprints? Could you post a screenshot (if using blueprints) or the code for the game instance? This will give us an idea of how it’s being created and what might be causing it to be constructed twice.

I’m sorry but I’m really lost here. How can be this a valid point? Do you know that GameInstance is constructed by the Engine itself, right?

I’m not creating it at all! (and shouldn’t) i’m only using the one created by the Engine (well, actually two of them because of the bug)

In the editor, if you go to Edit->Project Settings there is a category for Maps & Modes. At the bottom of the Maps & Modes menu is where the game instance to be used for the project can be set. For your project, is this set to the default (likely just called GameInstance) or have you changed it to something else? In blueprints it’s possible to create a custom game instance other than the default by choosing New Blueprint and setting the parent to GameInstance. This created blueprint can then be set as the game instance in the project settings.

If you are using a custom instance as described above then it is important to know how it was setup and what it was doing. If you are using the default game instance it is important to know what was changed/added to it for your game.

Because that’s the only way to reliably test your problem given the lack of information supplied. I asked earlier that you post code and you still haven’t. I’m just going to assume that your code/blueprints are the issue. Without any further information, ie code/screenshots, you are going to have to find and fix the issue yourself. There are many reasons why your game instance constructor is called more than once. Without information, people who want to help are forced to basically guess. If you haven’t solved it by now and still want help, you should really consider posting code/screenshots.