From 40e74f69440a833428356aff0638e0127fc95f77 Mon Sep 17 00:00:00 2001 From: Andyyy7666 <86536434+Andyyy7666@users.noreply.github.com> Date: Wed, 25 Oct 2023 21:47:43 +0200 Subject: [PATCH] fix: allow multiple callbacks of a resource --- shared/functions.lua | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/shared/functions.lua b/shared/functions.lua index 4bc42da..a19c4cf 100644 --- a/shared/functions.lua +++ b/shared/functions.lua @@ -1,25 +1,29 @@ local startedResources = {} -local callbacks = {} + +local function stateChanged(resourceName, state) + local callbacks = startedResources[resourceName] + if not callbacks then return end + for _, cb do + cb(state) + end +end AddEventHandler("onResourceStart", function(resourceName) - startedResources[resourceName] = true - local callback = callbacks[resourceName] - if not callback then return end - callback(true) + stateChanged(resourceName, true) end) AddEventHandler("onResourceStop", function(resourceName) - startedResources[resourceName] = nil - local callback = callbacks[resourceName] - if not callback then return end - callback(false) + stateChanged(resourceName, false) end) function NDCore.isResourceStarted(resourceName, cb) local started = GetResourceState(resourceName) == "started" if cb then - startedResources[resourceName] = started - callbacks[resourceName] = cb + if not startedResources[resourceName] then + startedResources[resourceName] = {} + end + local invokingResource = GetInvokingResource() + startedResources[resourceName][invokingResource] = cb cb(started) end return started