Skip to content

Commit 1bec280

Browse files
committed
Fixed issue #1, the second instance crashes with a fatal error (UE 5+)
1 parent 229ab67 commit 1bec280

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

CheckAppInstance.uplugin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
{
2020
"Name": "CheckAppInstance",
2121
"Type": "Runtime",
22-
"LoadingPhase": "Default",
22+
"LoadingPhase": "EarliestPossible",
2323
"WhitelistPlatforms": [
2424
"Win64"
2525
]

Source/CheckAppInstance/Private/CheckAppInstanceBPLibrary.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ UCheckAppInstanceBPLibrary::UCheckAppInstanceBPLibrary(const FObjectInitializer&
1111

1212
void UCheckAppInstanceBPLibrary::CheckAnotherAppInstance(bool bIsEnabled)
1313
{
14-
#if !UE_EDITOR
15-
// Only one instance of the game can be initialized!
16-
const UGeneralProjectSettings& ProjectSettings = *GetDefault<UGeneralProjectSettings>();
17-
const FString LockFilePath = FPlatformProcess::UserTempDir() + ProjectSettings.ProjectID.ToString();
18-
if (!IFileManager::Get().CreateFileWriter(*LockFilePath, 0) && bIsEnabled)
19-
{
20-
FPlatformApplicationMisc::RequestMinimize();
21-
FPlatformMisc::RequestExit(0);
22-
}
14+
#if !UE_EDITOR
15+
FCoreDelegates::OnAllModuleLoadingPhasesComplete.AddLambda([bIsEnabled]()
16+
{
17+
// Only one instance of the game can be initialized!
18+
const UGeneralProjectSettings& ProjectSettings = *GetDefault<UGeneralProjectSettings>();
19+
const FString LockFilePath = FPlatformProcess::UserTempDir() + ProjectSettings.ProjectID.ToString();
20+
if (!IFileManager::Get().CreateFileWriter(*LockFilePath, 0) && bIsEnabled)
21+
{
22+
FPlatformApplicationMisc::RequestMinimize();
23+
FPlatformMisc::RequestExit(0);
24+
}
25+
});
2326
#endif
2427
}
2528

Source/CheckAppInstance/Public/CheckAppInstanceBPLibrary.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
#include "Kismet/BlueprintFunctionLibrary.h"
66
#include "HAL/FileManager.h"
7-
#include "Runtime/ApplicationCore/Public/Windows/WindowsPlatformApplicationMisc.h"
7+
#include "HAL/PlatformApplicationMisc.h"
88
#include "GeneralProjectSettings.h"
9+
#include "Misc/CoreDelegates.h"
910
#include "CheckAppInstanceBPLibrary.generated.h"
1011

1112
UCLASS()

0 commit comments

Comments
 (0)