mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
Was going to split into separate imports, then remembered why I didn't in the first place. Improved annotations. Changed timeout into a for loop, and increase default to 500 ticks. Less variable reassignments. Allow functions to be called outside threads, but will not check if the request was successful (has loaded).
72 lines
2.7 KiB
Lua
72 lines
2.7 KiB
Lua
local function request(native, hasLoaded, requestType, name, timeout)
|
|
native(name)
|
|
|
|
if coroutine.running() then
|
|
for i = 1, timeout or 5 do
|
|
Wait(0)
|
|
if hasLoaded(name) then
|
|
return name
|
|
end
|
|
end
|
|
|
|
print(("Failed to load %s '%s' after %s ticks (%s)"):format(requestType, name, timeout, animDict))
|
|
end
|
|
|
|
return name
|
|
end
|
|
|
|
---Load an animation dictionary. When called from a thread, it will yield until it has loaded.
|
|
---@param animDict string
|
|
---@param timeout number? Number of ticks to wait for the dictionary to load. Default is 500.
|
|
---@return string? animDict
|
|
function lib.requestAnimDict(animDict, timeout)
|
|
if HasAnimDictLoaded(animDict) then return animDict end
|
|
|
|
if not DoesAnimDictExist(animDict) then
|
|
return error(("Attempted to load invalid animDict (%s)"):format(animDict))
|
|
end
|
|
|
|
return request(RequestAnimDict, HasAnimDictLoaded, 'animDict', animDict, timeout)
|
|
end
|
|
|
|
---Load an animation clipset. When called from a thread, it will yield until it has loaded.
|
|
---@param animSet string
|
|
---@param timeout number? Number of ticks to wait for the clipset to load. Default is 500.
|
|
---@return string? animSet
|
|
function lib.requestAnimSet(animSet, timeout)
|
|
if HasAnimSetLoaded(animSet) then return animSet end
|
|
return request(RequestAnimSet, HasAnimSetLoaded, 'animSet', animSet, timeout)
|
|
end
|
|
|
|
---Load a model. When called from a thread, it will yield until it has loaded.
|
|
---@param model number
|
|
---@param timeout number? Number of ticks to wait for the model to load. Default is 500.
|
|
---@return number? model
|
|
function lib.requestModel(model, timeout)
|
|
if not tonumber(model) then model = joaat(model) end
|
|
if HasModelLoaded(model) then return model end
|
|
|
|
if not IsModelValid(model) then
|
|
return error(("Attempted to load invalid model (%s)"):format(model))
|
|
end
|
|
|
|
return request(RequestModel, HasModelLoaded, 'model', model, timeout)
|
|
end
|
|
|
|
---Load a texture dictionary. When called from a thread, it will yield until it has loaded.
|
|
---@param textureDict string
|
|
---@param timeout number? Number of ticks to wait for the dictionary to load. Default is 500.
|
|
---@return string? textureDict
|
|
function lib.requestStreamedTextureDict(textureDict, timeout)
|
|
if HasStreamedTextureDictLoaded(textureDict) then return textureDict end
|
|
return request(RequestStreamedTextureDict, HasStreamedTextureDictLoaded, 'textureDict', textureDict, timeout)
|
|
end
|
|
|
|
---Load a named particle effect. When called from a thread, it will yield until it has loaded.
|
|
---@param fxName string
|
|
---@param timeout number? Number of ticks to wait for the particle effect to load. Default is 500.
|
|
---@return string? fxName
|
|
function lib.requestNamedPtfxAsset(fxName, timeout)
|
|
if HasNamedPtfxAssetLoaded(fxName) then return fxName end
|
|
return request(RequestNamedPtfxAsset, HasNamedPtfxAssetLoaded, 'fxName', fxName, timeout)
|
|
end |