-
Notifications
You must be signed in to change notification settings - Fork 197
/
UEHelpers.lua
283 lines (244 loc) · 10.1 KB
/
UEHelpers.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
local UEHelpers = {}
-- Uncomment the below require to use the Lua VM profiler on these functions
-- local jsb = require("jsbProfiler.jsbProfi")
-- Version 1 does not exist, we start at version 2 because the original version didn't have a version at all.
local Version = 3
-- Functions and classes local to this module, do not attempt to use!
---@param ObjectFullName string
---@param VariableName string
---@param ForceInvalidateCache boolean?
---@return UObject
local function CacheDefaultObject(ObjectFullName, VariableName, ForceInvalidateCache)
local DefaultObject = CreateInvalidObject()
if not ForceInvalidateCache then
DefaultObject = ModRef:GetSharedVariable(VariableName)
if DefaultObject and DefaultObject:IsValid() then return DefaultObject end
end
DefaultObject = StaticFindObject(ObjectFullName)
ModRef:SetSharedVariable(VariableName, DefaultObject)
if not DefaultObject:IsValid() then error(string.format("%s not found", ObjectFullName)) end
return DefaultObject
end
-- Everything in this section can be used in any mod that requires this module.
-- Exported functions -> START
function UEHelpers.GetUEHelpersVersion()
return Version
end
local EngineCache = CreateInvalidObject() ---@cast EngineCache UEngine
---Returns instance of UEngine
---@return UEngine
function UEHelpers.GetEngine()
if EngineCache:IsValid() then return EngineCache end
EngineCache = FindFirstOf("Engine") ---@cast EngineCache UEngine
return EngineCache
end
local GameInstanceCache = CreateInvalidObject() ---@cast GameInstanceCache UGameInstance
---Returns instance of UGameInstance
---@return UGameInstance
function UEHelpers.GetGameInstance()
if GameInstanceCache:IsValid() then return GameInstanceCache end
GameInstanceCache = FindFirstOf("GameInstance") ---@cast GameInstanceCache UGameInstance
return GameInstanceCache
end
---Returns the main UGameViewportClient (doesn't exist on a server)
---@return UGameViewportClient
function UEHelpers.GetGameViewportClient()
local Engine = UEHelpers.GetEngine()
if Engine:IsValid() and Engine.GameViewport then
return Engine.GameViewport
end
return CreateInvalidObject() ---@type UGameViewportClient
end
local PlayerControllerCache = CreateInvalidObject() ---@cast PlayerControllerCache APlayerController
---Returns first player controller.<br>
---In most games, a valid player controller is available from the start.<br>
---There are no player controllers on the server until a player joins the server.
---@return APlayerController
function UEHelpers.GetPlayerController()
if PlayerControllerCache:IsValid() then return PlayerControllerCache end
-- local Controllers = jsb.simpleBench("FindAllOf: PlayerController", FindAllOf, "PlayerController")
-- Controllers = jsb.simpleBench("FindAllOf: Controller", FindAllOf, "Controller")
local Controllers = FindAllOf("PlayerController") or FindAllOf("Controller") ---@type AController[]?
if Controllers then
for _, Controller in ipairs(Controllers) do
if Controller:IsValid() and (Controller.IsPlayerController and Controller:IsPlayerController() or Controller:IsLocalPlayerController()) then
PlayerControllerCache = Controller
break
end
end
end
return PlayerControllerCache
end
---Returns local player pawn
---@return APawn
function UEHelpers.GetPlayer()
local playerController = UEHelpers.GetPlayerController()
if playerController:IsValid() and playerController.Pawn then
return playerController.Pawn
end
return CreateInvalidObject() ---@type APawn
end
local WorldCache = CreateInvalidObject() ---@cast WorldCache UWorld
---Returns the main UWorld
---@return UWorld
function UEHelpers.GetWorld()
if WorldCache:IsValid() then return WorldCache end
local PlayerController = UEHelpers.GetPlayerController()
if PlayerController:IsValid() then
WorldCache = PlayerController:GetWorld()
else
local GameInstance = UEHelpers.GetGameInstance()
if GameInstance:IsValid() then
WorldCache = GameInstance:GetWorld()
end
end
return WorldCache
end
---Returns UWorld->PersistentLevel
---@return ULevel
function UEHelpers.GetPersistentLevel()
local World = UEHelpers.GetWorld()
if World:IsValid() and World.PersistentLevel then
return World.PersistentLevel
end
return CreateInvalidObject() ---@type ULevel
end
---Returns UWorld->AuthorityGameMode<br>
---The function doesn't guarantee it to be an AGameMode, as many games derive their own game modes directly from AGameModeBase!
---@return AGameModeBase
function UEHelpers.GetGameModeBase()
local World = UEHelpers.GetWorld()
if World:IsValid() and World.AuthorityGameMode then
return World.AuthorityGameMode
end
return CreateInvalidObject() ---@type AGameModeBase
end
---Returns UWorld->GameState<br>
---The function doesn't guarantee it to be an AGameState, as many games derive their own game states directly from AGameStateBase!
---@return AGameStateBase
function UEHelpers.GetGameStateBase()
local World = UEHelpers.GetWorld()
if World:IsValid() and World.GameState then
return World.GameState
end
return CreateInvalidObject() ---@type AGameStateBase
end
---Returns PersistentLevel->WorldSettings
---@return AWorldSettings
function UEHelpers.GetWorldSettings()
local PersistentLevel = UEHelpers.GetPersistentLevel()
if PersistentLevel:IsValid() and PersistentLevel.WorldSettings then
return PersistentLevel.WorldSettings
end
return CreateInvalidObject() ---@type AWorldSettings
end
--- Returns an object that's useable with UFunctions that have a WorldContext parameter.<br>
--- Prefer to use an actor that you already have access to whenever possible over this function.
--- Any UObject that has a GetWorld() function can be used as WorldContext.
---@return UObject
function UEHelpers.GetWorldContextObject()
return UEHelpers.GetWorld()
end
---Returns an array of all players APlayerState
---@return APlayerState[]
function UEHelpers.GetAllPlayerStates()
local PlayerStates = {}
local GameState = UEHelpers.GetGameStateBase()
if GameState:IsValid() and GameState.PlayerArray then
for i = 1, #GameState.PlayerArray do
table.insert(PlayerStates, GameState.PlayerArray[i])
end
end
return PlayerStates
end
---Returns all players as APawn.<br>
---You can use `IsA` function to check the type of APawn to make sure it's the player class of the game.
---@return APawn[]
function UEHelpers.GetAllPlayers()
local PlayerPawns = {}
local PlayerStates = UEHelpers.GetAllPlayerStates()
if PlayerStates then
for i = 1, #PlayerStates do
local Pawn = PlayerStates[i].PawnPrivate
if Pawn and Pawn:IsValid() then
table.insert(PlayerPawns, Pawn)
end
end
end
return PlayerPawns
end
---Returns hit actor from FHitResult.<br>
---The function handles the struct differance between UE4 and UE5
---@param HitResult FHitResult
---@return AActor|UObject
function UEHelpers.GetActorFromHitResult(HitResult)
if not HitResult or not HitResult:IsValid() then
return CreateInvalidObject() ---@type AActor
end
if UnrealVersion:IsBelow(5, 0) then
return HitResult.Actor:Get()
elseif UnrealVersion:IsBelow(5, 4) then
return HitResult.HitObjectHandle.Actor:Get()
end
return HitResult.HitObjectHandle.ReferenceObject:Get()
end
---@param ForceInvalidateCache boolean? # Force update the cache
---@return UGameplayStatics
function UEHelpers.GetGameplayStatics(ForceInvalidateCache)
---@type UGameplayStatics
return CacheDefaultObject("/Script/Engine.Default__GameplayStatics", "UEHelpers_GameplayStatics", ForceInvalidateCache)
end
---@param ForceInvalidateCache boolean? # Force update the cache
---@return UKismetSystemLibrary
function UEHelpers.GetKismetSystemLibrary(ForceInvalidateCache)
---@type UKismetSystemLibrary
return CacheDefaultObject("/Script/Engine.Default__KismetSystemLibrary", "UEHelpers_KismetSystemLibrary", ForceInvalidateCache)
end
---@param ForceInvalidateCache boolean? # Force update the cache
---@return UKismetMathLibrary
function UEHelpers.GetKismetMathLibrary(ForceInvalidateCache)
---@type UKismetMathLibrary
return CacheDefaultObject("/Script/Engine.Default__KismetMathLibrary", "UEHelpers_KismetMathLibrary", ForceInvalidateCache)
end
---@param ForceInvalidateCache boolean? # Force update the cache
---@return UKismetStringLibrary
function UEHelpers.GetKismetStringLibrary(ForceInvalidateCache)
---@type UKismetStringLibrary
return CacheDefaultObject("/Script/Engine.Default__KismetStringLibrary", "UEHelpers_KismetStringLibrary", ForceInvalidateCache)
end
---@param ForceInvalidateCache boolean? # Force update the cache
---@return UKismetTextLibrary
function UEHelpers.GetKismetTextLibrary(ForceInvalidateCache)
---@type UKismetTextLibrary
return CacheDefaultObject("/Script/Engine.Default__KismetTextLibrary", "UEHelpers_KismetTextLibrary", ForceInvalidateCache)
end
---@param ForceInvalidateCache boolean? # Force update the cache
---@return UGameMapsSettings
function UEHelpers.GetGameMapsSettings(ForceInvalidateCache)
---@type UGameMapsSettings
return CacheDefaultObject("/Script/EngineSettings.Default__GameMapsSettings", "UEHelpers_GameMapsSettings", ForceInvalidateCache)
end
---Returns found FName or "None" FName if the operation faled
---@param Name string
---@return FName
function UEHelpers.FindFName(Name)
return FName(Name, EFindName.FNAME_Find)
end
---Returns added FName or "None" FName if the operation faled
---@param Name string
---@return FName
function UEHelpers.AddFName(Name)
return FName(Name, EFindName.FNAME_Add)
end
---Tries to find existing FName, if it doesn't exist a new FName will be added to the pool
---@param Name string
---@return FName # Returns found or added FName, “None” FName if both operations fail
function UEHelpers.FindOrAddFName(Name)
local NameFound = FName(Name, EFindName.FNAME_Find)
if NameFound == NAME_None then
NameFound = FName(Name, EFindName.FNAME_Add)
end
return NameFound
end
-- Exported functions -> END
return UEHelpers