1+ -- ########################
2+ -- DEFINITIONS
3+ -- ########################
4+ -- state
15local 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+ -- ########################
620local 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
5966end
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
6785end )
0 commit comments