Skip to content

Commit

Permalink
feat: UEHelpers: Add GetGameInstance() function
Browse files Browse the repository at this point in the history
feat: UEHelpers: Add GetPlayer() function
feat: UEHelpers: Rework GetPlayerController() to get the player controller from first/main local player
  • Loading branch information
igromanru committed Sep 7, 2024
1 parent ee09c7c commit c824c42
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions assets/Mods/shared/UEHelpers/UEHelpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,8 @@ function UEHelpers.GetUEHelpersVersion()
return Version
end

local PlayerControllerCache = RemoteObject:new() ---@cast PlayerControllerCache APlayerController
--- Returns the first valid PlayerController that is currently controlled by a player.
---@return APlayerController
function UEHelpers.GetPlayerController()
if PlayerControllerCache:IsValid() then return PlayerControllerCache end
-- local PlayerControllers = jsb.simpleBench("findallof", FindAllOf, "Controller")
-- Uncomment line above and comment line below to profile this function
local PlayerControllers = FindAllOf("PlayerController") ---@type APlayerController[]?
if not PlayerControllers or #PlayerControllers < 1 then
print("GetPlayerController: No PlayerControllers were found\n")
return RemoteObject:new() ---@type APlayerController
end
for _, Controller in ipairs(PlayerControllers) do
if Controller.Pawn:IsValid() and Controller.Pawn:IsPlayerControlled() then
PlayerControllerCache = Controller
break
end
end
return PlayerControllerCache
end

local EngineCache = RemoteObject:new() ---@cast EngineCache UEngine
---Returns first valid instance of UEngine
---Returns instance of UEngine
---@return UEngine
function UEHelpers.GetEngine()
if EngineCache:IsValid() then return EngineCache end
Expand All @@ -74,6 +53,16 @@ function UEHelpers.GetEngine()
return EngineCache
end

local GameInstanceCache = RemoteObject:new() ---@cast GameInstanceCache UGameInstance
---Returns instance of UGameInstance
---@return UGameInstance
function UEHelpers.GetGameInstance()
if GameInstanceCache:IsValid() then return GameInstanceCache end

GameInstanceCache = FindFirstOf("GameInstance") ---@type UGameInstance
return GameInstanceCache
end

--- Returns the main UGameViewportClient
---@return UGameViewportClient
function UEHelpers.GetGameViewportClient()
Expand All @@ -84,6 +73,33 @@ function UEHelpers.GetGameViewportClient()
return RemoteObject:new() ---@type UGameViewportClient
end

local PlayerControllerCache = RemoteObject:new() ---@cast PlayerControllerCache APlayerController
---Returns first local player controller
---@return APlayerController
function UEHelpers.GetPlayerController()
if PlayerControllerCache:IsValid() then return PlayerControllerCache end

local GameInstance = UEHelpers.GetGameInstance()
if GameInstance:IsValid() and #GameInstance.LocalPlayers > 0 then
local localPlayer = GameInstance.LocalPlayers[1]
if localPlayer:IsValid() then
PlayerControllerCache = localPlayer.PlayerController
end
end

return PlayerControllerCache
end

---Returns local player pawn
---@return APawn
function UEHelpers.GetPlayer()
local playerController = UEHelpers.GetPlayerController()
if playerController:IsValid() then
return playerController.Pawn
end
return RemoteObject:new() ---@type APawn
end

local WorldCache = RemoteObject:new() ---@cast WorldCache UWorld
--- Returns the main UWorld
---@return UWorld
Expand Down Expand Up @@ -129,7 +145,7 @@ end
---@return UKismetMathLibrary
function UEHelpers.GetKismetMathLibrary(ForceInvalidateCache)
---@type UKismetMathLibrary
return CacheDefaultObject("/Script/Engine.Default__KismetMathLibrary", "UEHelpers_KismetMathLibrary", ForceInvalidateCache)
return CacheDefaultObject("/Script/Engine.Default__KismetMathLibrary", "UEHelpers_KismetMathLibrary", ForceInvalidateCache)
end

---@param ForceInvalidateCache boolean # Force update the cache
Expand Down

0 comments on commit c824c42

Please sign in to comment.