Updated to 3.0.1 and refactored a lot

This commit is contained in:
benjamyn committed 2024-08-27 12:39:08 +10:00
1 parent c160183700
commit f8401de9f8
6 files changed
+532 -336

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
---@class BenUtils
local BenUtils = {}
BenUtils.__index = BenUtils
local UEHelpers = require("UEHelpers")
local TextLib = UEHelpers:GetKismetTextLibrary()
-- Globals
local commands_registered = {}
-- End Globals
-- Internal functions
-- End internal functions
-- Utility Functions
---@return self
function BenUtils.new()
local self = setmetatable({}, BenUtils)
return self
end
function BenUtils.ShuffleInPlace(t)
for i = #t, 2, -1 do
local j = math.random(i)
t[i], t[j] = t[j], t[i]
end
end
---@param command string
---@param callback function
function BenUtils.RegisterCommand(command, callback)
commands_registered[command] = callback
-- AddRegisteredCommand(command, callback)
print(string.format("[BenUtils] Registered command %s", command))
end
---@param PlayerName string
function BenUtils.GetPlayerStateByName(PlayerName)
local crabs = FindAllOf("CrabPS")
for _, v in ipairs(crabs) do
-- local crab = v:get()
if v.PlayerNamePrivate:ToString() == PlayerName then
return v
end
end
return nil
end
---@param input string
function BenUtils.StringToFString(input)
-- I have to first convert the input to an FText
-- Then I can call the Conv_TextToString function to get an FSTring
local ftext_obj = FText(input)
return TextLib:Conv_TextToString(ftext_obj)
end
---@param PlayerName FString
---@param ChatMessage FString
function BenUtils.ParseChatCommand(self, PlayerName, ChatMessage)
local playername = PlayerName:get()
local chatmessage = ChatMessage:get()
local firstchar = string.sub(chatmessage:ToString(), 1, 1)
---@type table
local command = {}
if firstchar == "/" then
for i in string.gmatch(chatmessage:ToString(), "%S+") do
table.insert(command, i)
end
local cmd = string.sub(command[1], 2, #command[1])
-- local args = table.remove(command, 1)
if commands_registered[cmd] then
commands_registered[cmd](playername:ToString(), table.unpack(command, 2, #command))
end
end
end
-- End utility functions
-- Register Util hooks here
RegisterHook("/Script/CrabChampions.CrabPC:ClientOnReceivedChatMessage", BenUtils.ParseChatCommand)
-- End registry hooks
return BenUtils
+113
View File
@@ -0,0 +1,113 @@
local UEHelpers = {}
-- Uncomment the below require to use the Lua VM profiler on these functions
-- local jsb = require "jsbProfi"
-- Version 1 does not exist, we start at version 2 because the original version didn't have a version at all.
local Version = 2
-- Functions local to this module, do not attempt to use!
local CacheDefaultObject = function(ObjectFullName, VariableName, ForceInvalidateCache)
local DefaultObject
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
--- Returns the first valid PlayerController that is currently controlled by a player.
---@return APlayerController
local PlayerController = nil
function UEHelpers.GetPlayerController()
if PlayerController and PlayerController:IsValid() then return PlayerController end
-- local PlayerControllers = jsb.simpleBench("findallof", FindAllOf, "Controller")
-- Uncomment line above and comment line below to profile this function
local PlayerControllers = FindAllOf("PlayerController") or FindAllOf("Controller")
if not PlayerControllers then return Print("No PlayerControllers found\n") end
for _, Controller in pairs(PlayerControllers or {}) do
if Controller.Pawn:IsValid() and Controller.Pawn:IsPlayerControlled() then
PlayerController = Controller
break
-- else
-- print("Not valid or not player controlled\n")
end
end
if PlayerController and PlayerController:IsValid() then
return PlayerController
end
error("No PlayerController found\n")
end
--- Returns the UWorld that the player is currenlty in.
---@return UWorld
function UEHelpers.GetWorld()
return UEHelpers.GetPlayerController():GetWorld()
end
--- Returns the UGameViewportClient for the player.
---@return AActor
function UEHelpers.GetGameViewportClient()
return UEHelpers.GetPlayerController().Player.ViewportClient
end
--- Returns an object that's useable with UFunctions that have a WorldContextObject param.
--- Prefer to use an actor that you already have access to whenever possible over this function.
---@return AActor
function UEHelpers.GetWorldContextObject()
return UEHelpers.GetPlayerController()
end
function UEHelpers.GetGameplayStatics(ForceInvalidateCache)
return CacheDefaultObject("/Script/Engine.Default__GameplayStatics", "UEHelpers_GameplayStatics",
ForceInvalidateCache)
end
function UEHelpers.GetKismetSystemLibrary(ForceInvalidateCache)
return CacheDefaultObject("/Script/Engine.Default__KismetSystemLibrary", "UEHelpers_KismetSystemLibrary",
ForceInvalidateCache)
end
function UEHelpers.GetKismetMathLibrary(ForceInvalidateCache)
return CacheDefaultObject("/Script/Engine.Default__KismetMathLibrary", "UEHelpers_KismetMathLibrary",
ForceInvalidateCache)
end
function UEHelpers.GetKismetMathLibrary(ForceInvalidateCache)
return CacheDefaultObject("/Script/Engine.Default__KismetMathLibrary", "UEHelpers_KismetMathLibrary",
ForceInvalidateCache)
end
function UEHelpers.GetKismetTextLibrary(ForceInvalidateCache)
return CacheDefaultObject("/Script/Engine.Default__KismetTextLibrary", "UEHelpers_KismetTextLibrary",
ForceInvalidateCache)
end
function UEHelpers.GetGameMapsSettings(ForceInvalidateCache)
return CacheDefaultObject("/Script/EngineSettings.Default__GameMapsSettings", "UEHelpers_GameMapsSettings",
ForceInvalidateCache)
end
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