Skip to content

Commit 0d82c0a

Browse files
authored
refactor: improve console enabler initialization and configuration
- Move console key configuration to top-level constants - Replace object notification with non-persistent ClientRestart hook to reduce unnecessary hooks - Rename state variables for clarity (WasFirstConsoleCreated -> WasConsoleCreated) - Add IsDynamicViewport flag for dynamic viewport support - Add ability to unregister hook when console is created (non-dynamic viewports) - Improve code organization with section comments
2 parents 4a88719 + 5b5a8c6 commit 0d82c0a

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

  • assets/Mods/ConsoleEnablerMod/Scripts
Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1+
--########################
2+
-- DEFINITIONS
3+
--########################
4+
-- state
15
local UEHelpers = require("UEHelpers")
6+
local Pre, Post = -1, -1
7+
local WasConsoleCreated = false
8+
-- CONFIGURATION
9+
-- you can edit the key names to your liking, make sure they match UE names
10+
local IsDynamicViewport = false
11+
local KeysToAdd = {
12+
UEHelpers.FindFName("Tilde"),
13+
UEHelpers.FindFName("F10")
14+
}
215

3-
local PlayerControllerHookActive = false
4-
local WasFirstConsoleCreated = false
516

17+
--########################
18+
-- OWN LOGIC
19+
--########################
620
local function RemapConsoleKeys()
721
-- Change console key
822
local InputSettings = StaticFindObject("/Script/Engine.Default__InputSettings") ---@cast InputSettings UInputSettings
923
if not InputSettings:IsValid() then print("[ConsoleEnabler] InputSettings not found, could not change console key\n") return end
1024

1125
local ConsoleKeys = InputSettings.ConsoleKeys
12-
13-
local KeysToAdd = {
14-
UEHelpers.FindFName("Tilde"),
15-
UEHelpers.FindFName("F10")
16-
}
17-
1826
for _, KeyName in ipairs(KeysToAdd) do
1927
if KeyName ~= NAME_None then
2028
local KeyIsAlreadySet = false
2129
for i = 1, #ConsoleKeys do
2230
if ConsoleKeys[i].KeyName == KeyName then
2331
KeyIsAlreadySet = true
32+
break
2433
end
2534
end
2635
if not KeyIsAlreadySet then
@@ -40,28 +49,37 @@ local function CreateConsole()
4049

4150
local ConsoleClass = Engine.ConsoleClass ---@type UClass
4251
local GameViewport = Engine.GameViewport
43-
44-
if GameViewport:IsValid() and GameViewport.ViewportConsole:IsValid() then
52+
if (GameViewport:IsValid() and GameViewport.ViewportConsole:IsValid()) then
4553
-- Console already exists, let's just remap the keys
54+
WasConsoleCreated = true
4655
RemapConsoleKeys()
47-
elseif ConsoleClass:IsValid() and GameViewport:IsValid() then
56+
elseif (ConsoleClass:IsValid() and GameViewport:IsValid()) then
4857
local CreatedConsole = StaticConstructObject(ConsoleClass, GameViewport) ---@cast CreatedConsole UConsole
4958
if not CreatedConsole:IsValid() then print("[CreateConsole] Was unable to construct an UConsole object\n") return end
5059

5160
GameViewport.ViewportConsole = CreatedConsole
52-
PlayerControllerHookActive = true
53-
WasFirstConsoleCreated = true
54-
61+
WasConsoleCreated = true
5562
RemapConsoleKeys()
5663
else
5764
print("ConsoleClass, GameViewport, or ViewportConsole is invalid\n")
5865
end
5966
end
6067

61-
CreateConsole()
6268

63-
NotifyOnNewObject("/Script/Engine.PlayerController", function()
64-
if PlayerControllerHookActive or not WasFirstConsoleCreated then
69+
--########################
70+
-- ENTRY POINT
71+
--########################
72+
73+
ExecuteInGameThread(CreateConsole)
74+
75+
--- We only need to create console once since it is a VP singleton
76+
Pre, Post = RegisterHook("/Script/Engine.PlayerController:ClientRestart",
77+
---@param Context RemoteUnrealParam<APlayerController>
78+
function(Context)
79+
if (not WasConsoleCreated or IsDynamicViewport) then
6580
CreateConsole()
6681
end
82+
if (WasConsoleCreated and not IsDynamicViewport) then
83+
UnregisterHook("/Script/Engine.PlayerController:ClientRestart", Pre, Post)
84+
end
6785
end)

0 commit comments

Comments
 (0)