I am working on migrating a blueprint based game instance and some modes over to C++ code. In doing so I’ve learned that despite what game instance and modes are set in the project settings, they all run concurrently. This makes testing very tedious and nigh impossible as I can’t really tell what is what. How do I make it so that only one is running at a given moment?
Can you explain more by “the ALL run concurrently.” Do you mean the blueprint logic and the C++ logic are running at the same time?
If you still have the logic in your blueprints and the blueprints extend your C++ classes then you have the logic in twice. You should disconnect the logic from your blueprints to prevent them from running.
If that’s not the case, maybe your Game Mode is being overridden in your map and you aren’t using the game mode you think you are.
Yes, they all run at the same time.
Any C++ code I’m writing isn’t extending blueprints (or vice versa). I do not believe anything to be overridden, as multiple instances/modes/states are ticking and affecting what happens in the world during play. If something was being overridden, I would expect only one instance/mode/state to be functional.
How are you validating multiple modes and instances are running? That should be completely impossible on a fundamental level. Put some prints in your begin plays of all your blueprints and C++ modes/instances and see what fires.
The various HUD elements are working with one instance/mode that is based on blueprints. Discord rich presence is integrated into the blueprint game instance, and I’ve confirmed that it is communicating with their servers and updating my presence accordingly.
The teal text in the top left is being output from a C++ only game instance that has its own game mode that is trying to do things (checking actors placed in the world, etc.). It doesn’t inherit or communicate with blueprints at all.
Breakpoints are hit in the C++ only classes when a debugger is attached even if those aren’t the specified instance/mode in the configuration. As you say, this shouldn’t be happening.
Hmm, i think maybe we have a miscommunication on the definition of Game Mode. When i see that i think of the class Game Mode which there is only ever one at a time and it has no connection/reliance to the Game Instance.
Is that what you are talking about or are you just saying you have multiple modes in your game?
Also when you say instance do you mean the class Game Instance which is only assigned in the ini (project settings)?
To be more clear, I am under the impression that there are multiple game instances running concurrently during play for me. Additionally, I believe that multiple game modes (and their associated game states) are running concurrently. A game instance being a class that inherits the UGameInstance class, a game mode being a class that inherits from either AGameMode or AGameModeBase, and a game state being a class that inherits from either AGameState or AGameStateBase.
From my understanding, there should only be one game instance ever at run time. There could be multiple game modes, but only one should be running at a given moment.
The teal text in the top left is generated in two parts. The previous frame’s frametime is determined in a C++ only game instance. The Vec3 is reporting the position of an actor created in a C++ only game mode. When the actor is not found in the game world, the position is not listed on the screen.
Everything else shown in the HUD and UI (from second image) is created from a separate and distinct game instance and separate and distinct game modes (defined through blueprints) than the aforementioned C++ only classes.
Because I am seeing the result from one game instance (teal text) alongside the result of another game instance (Discord rich presence) I believe that two game instances are running concurrently.
Because I see the results of two game modes (unique actor spawned into the game world from one mode and a player with HUD from another) i believe that two game modes are running concurrently. I am at a loss of words in how to describe this any more thoroughly.
I need to know how to make it so only one of each type will run at a time.
I’m getting 3 UGameInstance running concurrently (verified by logging from its constructor). Is this possible?
Can you test this in PIE and Standalone, also making sure you are the only player (offline)?
I think the GameMode should only exist once (on the server), but I am not so sure about the GameInstance, which might be instantiated per client.
Documentation says if you run in PIE you get 1 GameInstance per instance:
I get this in a packaged game (verified by breakpoints in its constructor). I’m not using engine source, so I don’t see the stack where it would tell me who is instantiating me. I am using an empty map for a single player game. I am also sub classing UGameInstance along with some other game* classes.
I’m not sure where to look myself then I don’t think it is normal to have multiple game instances in that situation. I suppose you aren’t creating them somewhere deep in your own code… Tried testing this on a clean project without and with parts of your code?
Interesting. Well the fact that you say it shouldn’t happen, actually helps me alot. I’ll look deeper, because I must be forcing it to happen somehow. BTW, I get GameModeBase and GameInstance constructed when the editor starts up. before I press the play button!
Callstacks
UE4Editor-ZPOC.dll!UZGameInstance::UGameInstance() Line 23 C++
UE4Editor-CoreUObject.dll!UClass::CreateDefaultObject() Line 3791 C++
UE4Editor-CoreUObject.dll!UClass::GetDefaultObject(bool bCreateIfNeeded) Line 2955 C++
[Inline Frame] UE4Editor-KismetCompiler.dll!FKismetCompilerUtilities::CompileDefaultProperties(UClass *)
UE4Editor-ZPOC.dll!UGameInstance::UGameInstance() Line 23 C++
UE4Editor-CoreUObject.dll!UClass::CreateDefaultObject() Line 3791 C++
[Inline Frame] UE4Editor-CoreUObject.dll!UClass::GetDefaultObject(bool) Line 2952 C++
UE4Editor-CoreUObject.dll!FLinkerLoad::CreateExport(int Index) Line 4823 C++
> | UE4Editor-ZPOC.dll!UZGameInstance::UGameInstance() Line 23 | C++ |
---|---|---|
UE4Editor-CoreUObject.dll!UClass::CreateDefaultObject() Line 3791 | C++ | |
[Inline Frame] UE4Editor-CoreUObject.dll!UClass::GetDefaultObject(bool) Line 2952 | C++ | |
UE4Editor-CoreUObject.dll!UObjectLoadAllCompiledInDefaultProperties() Line 911 | C++ | |
UE4Editor-CoreUObject.dll!ProcessNewlyLoadedUObjects(FName Package, bool bCanProcessNewlyLoadedObjects) Line 1003 | C++ | |
[Inline Frame] UE4Editor-CoreUObject.dll!Invoke(void(*)(FName, bool) &) Line 51 | C++ |