-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement custom GameInstance initialization so that game knows wheth…
…er it is dedicated server by the time UGameInstance::Init is called
- Loading branch information
1 parent
98043de
commit c5dfc5e
Showing
5 changed files
with
87 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "UESTGameInstance.h" | ||
|
||
// God forgive me | ||
// UGameInstance::WorldContext is not public, and neither InitializeStandalone nor InitializeForPlayInEditor is suitable for us. | ||
// So we use this template hack to access WorldContext field. | ||
// Source: https://ledas.com/post/857-how-to-hack-c-with-templates-and-friends/ | ||
template<FWorldContext* UGameInstance::* WorldContext> | ||
struct Stealer { | ||
static friend FWorldContext*& FieldGetter(UGameInstance& GameInstance) { | ||
return GameInstance.*WorldContext; | ||
} | ||
}; | ||
|
||
template struct Stealer<&UGameInstance::WorldContext>; | ||
static FWorldContext*& FieldGetter(UGameInstance&); | ||
|
||
void IUESTGameInstance::DefaultInitializeForTests(UGameInstance& GameInstance, const bool bRunAsDedicated, const int32 PIEInstance) | ||
{ | ||
constexpr auto WorldType = EWorldType::Game; | ||
|
||
auto& WorldContext = GameInstance.GetEngine()->CreateNewWorldContext(WorldType); | ||
WorldContext.OwningGameInstance = &GameInstance; | ||
WorldContext.PIEInstance = PIEInstance; | ||
WorldContext.PIEPrefix = UWorld::BuildPIEPackagePrefix(PIEInstance); | ||
|
||
// We want to init this before calling UGameInstance::Init | ||
WorldContext.RunAsDedicated = bRunAsDedicated; | ||
|
||
FieldGetter(GameInstance) = &WorldContext; | ||
|
||
auto* DummyWorld = UWorld::CreateWorld(WorldType, true); | ||
DummyWorld->SetGameInstance(&GameInstance); | ||
WorldContext.SetCurrentWorld(DummyWorld); | ||
|
||
GameInstance.Init(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include "UESTGameInstance.generated.h" | ||
|
||
UINTERFACE() | ||
class UEST_API UUESTGameInstance : public UInterface | ||
{ | ||
GENERATED_BODY() | ||
}; | ||
|
||
/** | ||
* Your GameInstance class can implement this interface if you want custom initialization in tests. | ||
* If you don't, IUESTGameInstance::DefaultInitializeForTests is used. | ||
*/ | ||
class UEST_API IUESTGameInstance | ||
{ | ||
GENERATED_BODY() | ||
public: | ||
|
||
virtual void InitializeForTests(const bool bRunAsDedicated, int32 PIEInstance) = 0; | ||
|
||
static void DefaultInitializeForTests(UGameInstance& GameInstance, bool bRunAsDedicated, int32 PIEInstance); | ||
}; |