Refactored model/anim checks, utility functions, and text rendering

Modified EnsureModelLoaded to skip validation and request loops if the model is already loaded

Condensed SafeAddBlip, SafeRemoveBlip, etc. into dedicated utility functions

Removed AddTextEntryOnce and SafeAddTextEntry — native existence checks were redundant

Replaced repetitive table inserts with keyed tables using tostring(number) to avoid unnecessary loops

Added HasAnimDictLoaded check to RequestAnimDict to prevent redundant request loops

Introduced FloatingHelpText in utilities (aesthetic improvement)

Updated DrawText: replaced outdated logic, switched 1 to true for clarity/preference
This commit is contained in:
MrNewb committed 2025-08-07 01:50:50 -04:00
1 parent b1556d13fb
commit a63837bd1f
1 file changed
+62 -116
+62 -116
View File
@@ -1,12 +1,9 @@
---@class Utility
Utility = Utility or {} Utility = Utility or {}
local blipIDs = {} local blipIDs = {}
local spawnedPeds = {} local spawnedPeds = {}
Locales = Locales or Require('modules/locales/shared.lua') Locales = Locales or Require('modules/locales/shared.lua')
-- === Local Helpers ===
---Get the hash of a model (string or number) ---Get the hash of a model (string or number)
---@param model string|number ---@param model string|number
---@return number ---@return number
@@ -22,6 +19,7 @@ end
---@return boolean, number ---@return boolean, number
local function ensureModelLoaded(model) local function ensureModelLoaded(model)
local hash = getModelHash(model) local hash = getModelHash(model)
if HasModelLoaded(hash) then return true, hash end
if not IsModelValid(hash) and not IsModelInCdimage(hash) then return false, hash end if not IsModelValid(hash) and not IsModelInCdimage(hash) then return false, hash end
RequestModel(hash) RequestModel(hash)
local count = 0 local count = 0
@@ -32,84 +30,6 @@ local function ensureModelLoaded(model)
return HasModelLoaded(hash), hash return HasModelLoaded(hash), hash
end end
---Add a text entry if possible
---@param key string
---@param text string
local function addTextEntryOnce(key, text)
if not AddTextEntry then return end
AddTextEntry(key, text)
end
---Create a blip safely and store its reference
---@param coords vector3
---@param sprite number
---@param color number
---@param scale number
---@param label string
---@param shortRange boolean
---@param displayType number
---@return number
local function safeAddBlip(coords, sprite, color, scale, label, shortRange, displayType)
local blip = AddBlipForCoord(coords.x, coords.y, coords.z)
SetBlipSprite(blip, sprite or 8)
SetBlipColour(blip, color or 3)
SetBlipScale(blip, scale or 0.8)
SetBlipDisplay(blip, displayType or 2)
SetBlipAsShortRange(blip, shortRange)
addTextEntryOnce(label, label)
BeginTextCommandSetBlipName(label)
EndTextCommandSetBlipName(blip)
table.insert(blipIDs, blip)
return blip
end
---Create a entiyty blip safely and store its reference
---@param entity number
---@param sprite number
---@param color number
---@param scale number
---@param label string
---@param shortRange boolean
---@param displayType number
---@return number
local function safeAddEntityBlip(entity, sprite, color, scale, label, shortRange, displayType)
local blip = AddBlipForEntity(entity)
SetBlipSprite(blip, sprite or 8)
SetBlipColour(blip, color or 3)
SetBlipScale(blip, scale or 0.8)
SetBlipDisplay(blip, displayType or 2)
SetBlipAsShortRange(blip, shortRange)
ShowHeadingIndicatorOnBlip(blip, true)
addTextEntryOnce(label, label)
BeginTextCommandSetBlipName(label)
EndTextCommandSetBlipName(blip)
table.insert(blipIDs, blip)
return blip
end
---Remove a blip safely from the stored list
---@param blip number
---@return boolean
local function safeRemoveBlip(blip)
for i, storedBlip in ipairs(blipIDs) do
if storedBlip == blip then
RemoveBlip(storedBlip)
table.remove(blipIDs, i)
return true
end
end
return false
end
---Add a text entry if possible (shortcut)
---@param text string
local function safeAddTextEntry(text)
if not AddTextEntry then return end
AddTextEntry(text, text)
end
-- === Public Utility Functions ===
---Create a prop with the given model and coordinates ---Create a prop with the given model and coordinates
---@param model string|number ---@param model string|number
---@param coords vector3 ---@param coords vector3
@@ -147,11 +67,7 @@ function Utility.CreateVehicle(model, coords, heading, networked)
SetVehicleNeedsToBeHotwired(vehicle, false) SetVehicleNeedsToBeHotwired(vehicle, false)
SetVehRadioStation(vehicle, "OFF") SetVehRadioStation(vehicle, "OFF")
SetModelAsNoLongerNeeded(hash) SetModelAsNoLongerNeeded(hash)
return vehicle, { return vehicle, {networkid = NetworkGetNetworkIdFromEntity(vehicle) or 0,coords = GetEntityCoords(vehicle),heading = GetEntityHeading(vehicle),}
networkid = NetworkGetNetworkIdFromEntity(vehicle) or 0,
coords = GetEntityCoords(vehicle),
heading = GetEntityHeading(vehicle),
}
end end
---Create a ped with the given model and coordinates ---Create a ped with the given model and coordinates
@@ -166,7 +82,7 @@ function Utility.CreatePed(model, coords, heading, networked, settings)
if not loaded then return nil, Prints and Prints.Error and Prints.Error("Model Has Not Loaded") end if not loaded then return nil, Prints and Prints.Error and Prints.Error("Model Has Not Loaded") end
local spawnedEntity = CreatePed(0, hash, coords.x, coords.y, coords.z, heading, networked, false) local spawnedEntity = CreatePed(0, hash, coords.x, coords.y, coords.z, heading, networked, false)
SetModelAsNoLongerNeeded(hash) SetModelAsNoLongerNeeded(hash)
table.insert(spawnedPeds, spawnedEntity) spawnedPeds[tostring(spawnedEntity)] = true
return spawnedEntity return spawnedEntity
end end
@@ -174,7 +90,7 @@ end
---@param text string ---@param text string
---@return boolean ---@return boolean
function Utility.StartBusySpinner(text) function Utility.StartBusySpinner(text)
safeAddTextEntry(text) AddTextEntry(text, text)
BeginTextCommandBusyString(text) BeginTextCommandBusyString(text)
AddTextComponentSubstringPlayerName(text) AddTextComponentSubstringPlayerName(text)
EndTextCommandBusyString(0) EndTextCommandBusyString(0)
@@ -201,7 +117,17 @@ end
---@param displayType number ---@param displayType number
---@return number ---@return number
function Utility.CreateBlip(coords, sprite, color, scale, label, shortRange, displayType) function Utility.CreateBlip(coords, sprite, color, scale, label, shortRange, displayType)
return safeAddBlip(coords, sprite, color, scale, label, shortRange, displayType) local blip = AddBlipForCoord(coords.x, coords.y, coords.z)
SetBlipSprite(blip, sprite or 8)
SetBlipColour(blip, color or 3)
SetBlipScale(blip, scale or 0.8)
SetBlipDisplay(blip, displayType or 2)
SetBlipAsShortRange(blip, shortRange)
AddTextEntry(label, label)
BeginTextCommandSetBlipName(label)
EndTextCommandSetBlipName(blip)
blipIDs[tostring(blip)] = true
return blip
end end
---Create a blip on the provided entity ---Create a blip on the provided entity
@@ -214,14 +140,28 @@ end
---@param displayType number ---@param displayType number
---@return number ---@return number
function Utility.CreateEntityBlip(entity, sprite, color, scale, label, shortRange, displayType) function Utility.CreateEntityBlip(entity, sprite, color, scale, label, shortRange, displayType)
return safeAddEntityBlip(entity, sprite, color, scale, label, shortRange, displayType) local blip = AddBlipForEntity(entity)
SetBlipSprite(blip, sprite or 8)
SetBlipColour(blip, color or 3)
SetBlipScale(blip, scale or 0.8)
SetBlipDisplay(blip, displayType or 2)
SetBlipAsShortRange(blip, shortRange)
ShowHeadingIndicatorOnBlip(blip, true)
AddTextEntry(label, label)
BeginTextCommandSetBlipName(label)
EndTextCommandSetBlipName(blip)
blipIDs[tostring(blip)] = true
return blip
end end
---Remove a blip if it exists ---Remove a blip if it exists
---@param blip number ---@param blip number
---@return boolean ---@return boolean
function Utility.RemoveBlip(blip) function Utility.RemoveBlip(blip)
return safeRemoveBlip(blip) if not blipIDs[tostring(blip)] then return false end
RemoveBlip(blip)
blipIDs[tostring(blip)] = nil
return true
end end
---Load a model into memory ---Load a model into memory
@@ -235,6 +175,7 @@ end
---@param dict string ---@param dict string
---@return boolean ---@return boolean
function Utility.RequestAnimDict(dict) function Utility.RequestAnimDict(dict)
if HasAnimDictLoaded(dict) then return true end
RequestAnimDict(dict) RequestAnimDict(dict)
local count = 0 local count = 0
while not HasAnimDictLoaded(dict) and count < 30000 do while not HasAnimDictLoaded(dict) and count < 30000 do
@@ -248,18 +189,12 @@ end
---@param entity number ---@param entity number
---@return boolean ---@return boolean
function Utility.RemovePed(entity) function Utility.RemovePed(entity)
local success = false if not spawnedPeds[tostring(entity)] then return false end
if DoesEntityExist(entity) then spawnedPeds[tostring(entity)] = nil
DeleteEntity(entity) if not DoesEntityExist(entity) then return false end
end SetEntityAsMissionEntity(entity, true, true)
for i, storedEntity in ipairs(spawnedPeds) do DeleteEntity(entity)
if storedEntity == entity then return true
table.remove(spawnedPeds, i)
success = true
break
end
end
return success
end end
---Show a native input menu and return the result ---Show a native input menu and return the result
@@ -269,7 +204,7 @@ end
function Utility.NativeInputMenu(text, length) function Utility.NativeInputMenu(text, length)
local maxLength = Math and Math.Clamp and Math.Clamp(length, 1, 50) or math.min(math.max(length or 10, 1), 50) local maxLength = Math and Math.Clamp and Math.Clamp(length, 1, 50) or math.min(math.max(length or 10, 1), 50)
local menuText = text or 'enter text' local menuText = text or 'enter text'
safeAddTextEntry(menuText) AddTextEntry(menuText, menuText)
DisplayOnscreenKeyboard(1, menuText, "", "", "", "", "", maxLength) DisplayOnscreenKeyboard(1, menuText, "", "", "", "", "", maxLength)
while (UpdateOnscreenKeyboard() == 0) do while (UpdateOnscreenKeyboard() == 0) do
DisableAllControlActions(0) DisableAllControlActions(0)
@@ -315,7 +250,8 @@ function Utility.ReloadSkin()
local ped = PlayerPedId() local ped = PlayerPedId()
local skinData = Utility.GetEntitySkinData(ped) local skinData = Utility.GetEntitySkinData(ped)
Utility.SetEntitySkinData(ped, skinData) Utility.SetEntitySkinData(ped, skinData)
for _, props in pairs(GetGamePool("CObject")) do local poolObject = GetGamePool("CObject")
for _, props in pairs(poolObject) do
if IsEntityAttachedToEntity(ped, props) then if IsEntityAttachedToEntity(ped, props) then
SetEntityAsMissionEntity(props, true, true) SetEntityAsMissionEntity(props, true, true)
DeleteObject(props) DeleteObject(props)
@@ -329,11 +265,23 @@ end
---@param text string ---@param text string
---@param duration number ---@param duration number
function Utility.HelpText(text, duration) function Utility.HelpText(text, duration)
safeAddTextEntry(text) AddTextEntry(text, text)
BeginTextCommandDisplayHelp(text) BeginTextCommandDisplayHelp(text)
EndTextCommandDisplayHelp(0, false, true, duration or 5000) EndTextCommandDisplayHelp(0, false, true, duration or 5000)
end end
---Show a floating help text in the world
---@param text string The text to show
---@param coords table The coords to show the message at
---@return nil
function Utility.FloatingHelpText(text, coords)
AddTextEntry("community_bridge_"..text, "community_bridge_"..text)
SetFloatingHelpTextWorldPosition(1, coords.x, coords.y, coords.z)
SetFloatingHelpTextStyle(1, 1, 2, -1, 3, 0)
BeginTextCommandDisplayHelp("community_bridge_"..text)
EndTextCommandDisplayHelp(2, false, false, -1)
end
---Draw 3D help text in the world ---Draw 3D help text in the world
---@param coords vector3 ---@param coords vector3
---@param text string ---@param text string
@@ -343,10 +291,10 @@ function Utility.Draw3DHelpText(coords, text, scale)
if onScreen then if onScreen then
SetTextScale(scale or 0.35, scale or 0.35) SetTextScale(scale or 0.35, scale or 0.35)
SetTextFont(4) SetTextFont(4)
SetTextProportional(1) SetTextProportional(true)
SetTextColour(255, 255, 255, 215) SetTextColour(255, 255, 255, 215)
SetTextEntry("STRING") SetTextEntry("STRING")
SetTextCentre(1) SetTextCentre(true)
AddTextComponentString(text) AddTextComponentString(text)
DrawText(x, y) DrawText(x, y)
local factor = (string.len(text)) / 370 local factor = (string.len(text)) / 370
@@ -357,7 +305,7 @@ end
---Show a native notification ---Show a native notification
---@param text string ---@param text string
function Utility.NotifyText(text) function Utility.NotifyText(text)
safeAddTextEntry(text) AddTextEntry(text, text)
SetNotificationTextEntry(text) SetNotificationTextEntry(text)
DrawNotification(false, true) DrawNotification(false, true)
end end
@@ -376,9 +324,7 @@ function Utility.TeleportPlayer(coords, conditionFunction, afterTeleportFunction
DoScreenFadeOut(2500) DoScreenFadeOut(2500)
Wait(2500) Wait(2500)
SetEntityCoords(ped, coords.x, coords.y, coords.z, false, false, false, false) SetEntityCoords(ped, coords.x, coords.y, coords.z, false, false, false, false)
if coords.w then if coords.w then SetEntityHeading(ped, coords.w) end
SetEntityHeading(ped, coords.w)
end
FreezeEntityPosition(ped, true) FreezeEntityPosition(ped, true)
local count = 0 local count = 0
while not HasCollisionLoadedAroundEntity(ped) and count <= 30000 do while not HasCollisionLoadedAroundEntity(ped) and count <= 30000 do
@@ -627,13 +573,13 @@ end
AddEventHandler('onResourceStop', function(resource) AddEventHandler('onResourceStop', function(resource)
if resource ~= GetCurrentResourceName() then return end if resource ~= GetCurrentResourceName() then return end
for _, blip in pairs(blipIDs) do for _, blip in pairs(blipIDs) do
if blip and DoesBlipExist(blip) then if blip and DoesBlipExist(tonumber(blip)) then
RemoveBlip(blip) RemoveBlip(tonumber(blip))
end end
end end
for _, ped in pairs(spawnedPeds) do for _, ped in pairs(spawnedPeds) do
if ped and DoesEntityExist(ped) then if ped and DoesEntityExist(tonumber(ped)) then
DeleteEntity(ped) DeleteEntity(tonumber(ped))
end end
end end
end) end)