From 04b25e2c065b0310ece3f7790435ce8cdf8c93e7 Mon Sep 17 00:00:00 2001 From: Jim Shield Date: Mon, 12 May 2025 22:59:31 +0100 Subject: [PATCH] create `waitForSharedLoad()` function This my attempt to aid ESX loading, essentially my scripts server side now need to be locked behind `onResourceStart()` because of ESX Loading ESX into my scripts was delaying half of it and breaking the other half This function forcibly waits until everythings loaded and then continues This shouldn't affect other frameworks loading ability My tests so far have been a success with it --- shared/_loaders.lua | 36 ++++++++++++++++++++++++++++++++++-- shared/helpers.lua | 2 +- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/shared/_loaders.lua b/shared/_loaders.lua index 611908f..9c2c92e 100644 --- a/shared/_loaders.lua +++ b/shared/_loaders.lua @@ -50,7 +50,13 @@ function onPlayerLoaded(func, onStart) AddEventHandler('QBCore:Client:OnPlayerLoaded', tempFunc) elseif isStarted(ESXExport) then onPlayerFramework = ESXExport - AddEventHandler('esx:playerLoaded', tempFunc) + AddEventHandler('esx:playerLoaded', function() + if waitForSharedLoad() then + if isStarted(ESXExport) then Wait(11000) end + tempFunc() + end + end + ) elseif isStarted(OXCoreExport) then onPlayerFramework = OXCoreExport AddEventHandler('ox:playerLoaded', tempFunc) @@ -103,7 +109,11 @@ function onResourceStart(func, thisScript) debugPrint("^6Bridge^7: ^2Registering ^3onResourceStart^7()") AddEventHandler('onResourceStart', function(resourceName) if getScript() == resourceName and (thisScript or true) then - func() + if waitForSharedLoad() then + print("^6Bridge^7: ^2Shared Load Detected^7.") + if isStarted(ESXExport) then Wait(10000) end + func() + end end end) end @@ -174,4 +184,26 @@ function waitForLogin() debugPrint("^6Bridge^7: ^2Player Login Detected^7.") return true end +end + +function waitForSharedLoad() + local timeout = 100000 -- 10 seconds in milliseconds + local startTime = GetGameTimer() + local loaded = true + while ((not Jobs or not next(Jobs)) and (not Items or not next(Items)) and (not Vehicles or not next(Vehicles))) and (GetGameTimer() - startTime) < timeout do + print((GetGameTimer() - startTime) < timeout) + Wait(1000) + debugPrint("Waiting for Jobs, Items, and Vehicles to be loaded") + if Jobs and Items and Vehicles then + print("^6Bridge^7: ^2Jobs, Items, and Vehicles Loaded^7.") + loaded = true + break + end + end + if not loaded then + print("^4Error^7: ^2Timeout reached while waiting for shared load^7.") + return false + else + return true + end end \ No newline at end of file diff --git a/shared/helpers.lua b/shared/helpers.lua index 7621c50..158be0b 100644 --- a/shared/helpers.lua +++ b/shared/helpers.lua @@ -262,7 +262,7 @@ end --- ``` function countTable(tbl) local i = 0 - for _ in pairs(tbl) do i = i + 1 end + for _ in pairs(tbl) do i += 1 end return i end