GameInstanceClass
GameInstanceClass
#Overview
name: GameInstanceClass
The value of this variable can be defined or overridden in .ini config files. 2
.ini config files referencing this setting variable.
It is referenced in 9
C++ source files.
#Summary
#Usage in the C++ source code
The purpose of GameInstanceClass is to specify the class to be used when instantiating the transient GameInstance object in Unreal Engine. This setting is crucial for initializing the game’s core functionality and managing the overall game state.
GameInstanceClass is primarily used by the Engine and Editor subsystems, specifically in the game initialization process and during Play-in-Editor (PIE) sessions. The main modules that rely on this variable are:
- Engine module
- UnrealEd module
- EngineSettings module
The value of this variable is typically set in the project settings, specifically in the GameMapsSettings configuration. It can be accessed and modified through the UGameMapsSettings class.
GameInstanceClass interacts with other important variables and systems, such as:
- GameDefaultMap
- GlobalDefaultGameMode
- GlobalDefaultServerGameMode
Developers should be aware of the following when using this variable:
- If an invalid class is specified, the engine will fall back to the default UGameInstance class.
- Changing this value can have significant impacts on the game’s core functionality and behavior.
- The specified class must inherit from UGameInstance.
Best practices when using GameInstanceClass include:
- Always ensure the specified class is valid and inherits from UGameInstance.
- Use this setting to implement game-wide systems and manage overall game state.
- Be cautious when changing this value, as it can affect the entire game structure.
- Test thoroughly after modifying this setting to ensure all game systems continue to function correctly.
- Document any custom GameInstance class implementation to maintain code clarity and ease future development.
#Setting Variables
#References In INI files
Location: <Workspace>/Engine/Config/BaseEngine.ini:10, section: [/Script/EngineSettings.GameMapsSettings]
- INI Section:
/Script/EngineSettings.GameMapsSettings
- Raw value:
/Script/Engine.GameInstance
- Is Array:
False
Location: <Workspace>/Projects/Lyra/Config/DefaultEngine.ini:67, section: [/Script/EngineSettings.GameMapsSettings]
- INI Section:
/Script/EngineSettings.GameMapsSettings
- Raw value:
/Game/B_LyraGameInstance.B_LyraGameInstance_C
- Is Array:
False
#References in C++ code
#Callsites
This variable is referenced in the following C++ source code:
#Loc: <Workspace>/Engine/Plugins/Tests/CQTest/Source/CQTest/Private/Components/PIENetworkTestStateRestorer.cpp:16
Scope (from outer to inner):
file
function FPIENetworkTestStateRestorer::FPIENetworkTestStateRestorer
Source code excerpt:
if (GameMapSettings)
{
OriginalGameInstance = GameMapSettings->GameInstanceClass;
GameMapSettings->GameInstanceClass = InGameInstanceClass;
}
else
{
UE_LOG(LogPieNetworkStateRestorer, Error, TEXT("Unable to get UGameMapsSettings in when creating FPIENetworkTestStateRestorer"));
}
}
#Loc: <Workspace>/Engine/Plugins/Tests/CQTest/Source/CQTest/Private/Components/PIENetworkTestStateRestorer.cpp:47
Scope (from outer to inner):
file
function void FPIENetworkTestStateRestorer::Restore
Source code excerpt:
if (GameMapSettings)
{
GameMapSettings->GameInstanceClass = OriginalGameInstance;
}
else
{
UE_LOG(LogPieNetworkStateRestorer, Error, TEXT("Unable to get UGameMapsSettings when destroying FPIENetworkTestStateRestorer"));
}
}
#Loc: <Workspace>/Engine/Plugins/Tests/CQTest/Source/CQTest/Public/Components/PIENetworkComponent.h:109
Scope (from outer to inner):
file
class class FNetworkComponentBuilder
Source code excerpt:
FPacketSimulationSettings* PacketSimulationSettings = nullptr;
TSubclassOf<AGameModeBase> GameMode = TSubclassOf<AGameModeBase>(nullptr);
FSoftClassPath GameInstanceClass = FSoftClassPath{};
int32 ClientCount = 2;
bool bIsDedicatedServer = true;
};
constexpr static uint32 NetworkTestContext = EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter;
#Loc: <Workspace>/Engine/Plugins/Tests/CQTest/Source/CQTest/Public/Impl/PIENetworkComponent.inl:200
Scope (from outer to inner):
file
function inline FNetworkComponentBuilder<NetworkDataType>& FNetworkComponentBuilder<NetworkDataType>::WithGameInstanceClass
Source code excerpt:
inline FNetworkComponentBuilder<NetworkDataType>& FNetworkComponentBuilder<NetworkDataType>::WithGameInstanceClass(FSoftClassPath InGameInstanceClass)
{
GameInstanceClass = InGameInstanceClass;
return *this;
}
template<typename NetworkDataType>
inline void FNetworkComponentBuilder<NetworkDataType>::Build(FPIENetworkComponent<NetworkDataType>& OutNetwork)
{
#Loc: <Workspace>/Engine/Plugins/Tests/CQTest/Source/CQTest/Public/Impl/PIENetworkComponent.inl:222
Scope (from outer to inner):
file
function inline void FNetworkComponentBuilder<NetworkDataType>::Build
Source code excerpt:
OutNetwork.PacketSimulationSettings = PacketSimulationSettings;
OutNetwork.GameMode = GameMode;
OutNetwork.StateRestorer = FPIENetworkTestStateRestorer{GameInstanceClass, GameMode};
}
#Loc: <Workspace>/Engine/Source/Editor/UnrealEd/Private/CookOnTheFlyServer.cpp:9336
Scope (from outer to inner):
file
function void UCookOnTheFlyServer::GetGameDefaultObjects
Source code excerpt:
AddDefaultObject(FName(TEXT("GlobalDefaultGameMode")));
AddDefaultObject(FName(TEXT("GlobalDefaultServerGameMode")));
AddDefaultObject(FName(TEXT("GameInstanceClass")));
}
}
bool UCookOnTheFlyServer::IsCookByTheBookRunning() const
{
return IsCookByTheBookMode() && IsInSession();
#Loc: <Workspace>/Engine/Source/Editor/UnrealEd/Private/PlayLevel.cpp:2939
Scope (from outer to inner):
file
function UGameInstance* UEditorEngine::CreateInnerProcessPIEGameInstance
Source code excerpt:
{
// Create a GameInstance for this new instance.
FSoftClassPath GameInstanceClassName = GetDefault<UGameMapsSettings>()->GameInstanceClass;
UClass* GameInstanceClass = GameInstanceClassName.TryLoadClass<UGameInstance>();
// If an invalid class type was specified we fall back to the default.
if (!GameInstanceClass)
{
GameInstanceClass = UGameInstance::StaticClass();
}
UGameInstance* GameInstance = NewObject<UGameInstance>(this, GameInstanceClass);
// We need to temporarily add the GameInstance to the root because the InitializeForPlayInEditor
// call can do garbage collection wiping out the GameInstance
GameInstance->AddToRoot();
// Attempt to initialize the GameInstance. This will construct the world.
#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/GameEngine.cpp:1138
Scope (from outer to inner):
file
function void UGameEngine::Init
Source code excerpt:
{
TRACE_CPUPROFILER_EVENT_SCOPE(InitGameInstance);
FSoftClassPath GameInstanceClassName = GetDefault<UGameMapsSettings>()->GameInstanceClass;
UClass* GameInstanceClass = (GameInstanceClassName.IsValid() ? LoadObject<UClass>(NULL, *GameInstanceClassName.ToString()) : UGameInstance::StaticClass());
if (GameInstanceClass == nullptr)
{
UE_LOG(LogEngine, Error, TEXT("Unable to load GameInstance Class '%s'. Falling back to generic UGameInstance."), *GameInstanceClassName.ToString());
GameInstanceClass = UGameInstance::StaticClass();
}
GameInstance = NewObject<UGameInstance>(this, GameInstanceClass);
GameInstance->InitializeStandalone();
}
// // Creates the initial world context. For GameEngine, this should be the only WorldContext that ever gets created.
// FWorldContext& InitialWorldContext = CreateNewWorldContext(EWorldType::Game);
#Loc: <Workspace>/Engine/Source/Runtime/EngineSettings/Classes/GameMapsSettings.h:189
Scope (from outer to inner):
file
class class UGameMapsSettings : public UObject
Source code excerpt:
/** The class to use when instantiating the transient GameInstance class */
UPROPERTY(config, noclear, EditAnywhere, Category=GameInstance, meta=(MetaClass="/Script/Engine.GameInstance"))
FSoftClassPath GameInstanceClass;
private:
/** The map that will be loaded by default when no other map is loaded. */
UPROPERTY(config, EditAnywhere, Category=DefaultMaps, meta=(AllowedClasses="/Script/Engine.World"))
FSoftObjectPath GameDefaultMap;