From ce90c6bc47e9f2cbe480310fe250de6b7f55e384 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Mon, 7 Aug 2023 21:45:58 +0200 Subject: [PATCH 1/2] fix(ESX.UI): Close Opened Menus on Resource Stop I have encountered several frustrating instances when accidentally restarting a script that still had an open ESX UI. This situation prevents the UI from closing properly and leads to a cascade of errors, ultimately forcing a game restart. To address this issue, I propose the following code changes. The code introduces an event handler for the onResourceStop event, enabling us to loop through all open ESX UIs and identify any opened menus within the same namespace as the resource name. Following the best practice, the namespace should ideally align with the resource name. I believe implementing these changes will provide a smoother user experience. I look forward to your feedback and merging these improvements into the main branch. --- [core]/es_extended/client/functions.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/[core]/es_extended/client/functions.lua b/[core]/es_extended/client/functions.lua index af1c9183..9e8656de 100644 --- a/[core]/es_extended/client/functions.lua +++ b/[core]/es_extended/client/functions.lua @@ -1343,6 +1343,17 @@ AddEventHandler('esx:showHelpNotification', function(msg, thisFrame, beep, durat ESX.ShowHelpNotification(msg, thisFrame, beep, duration) end) +AddEventHandler('onResourceStop', function(resourceName) + for i = 1, #ESX.UI.Menu.Opened, 1 do + if ESX.UI.Menu.Opened[i] then + if ESX.UI.Menu.Opened[i].namespace == resourceName then + ESX.UI.Menu.Opened[i].close() + ESX.UI.Menu.Opened[i] = nil + end + end + end +end) + ---@param model number|string ---@return string function ESX.GetVehicleType(model) From 89a75bcc398172d4a5122aa5628e29feb98853e7 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Mon, 14 Aug 2023 23:29:24 +0200 Subject: [PATCH 2/2] Eliminate the need for matching namespace & resource name When the Menu.Open function is called, a new key-value pair labeled "resourceName" is now added to the menu object. This addition serves the purpose of capturing and storing the invoking resource's name that triggers the function by calling the GetInvokingResource() native. By implementing this change, we establish a more direct and reliable method for checking the "resourceName" key within the menu object. This approach eliminates the need to solely rely on matching the namespace and resource name. As a result, the code becomes more robust and less prone to errors. --- [core]/es_extended/client/functions.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/[core]/es_extended/client/functions.lua b/[core]/es_extended/client/functions.lua index 9e8656de..7887baab 100644 --- a/[core]/es_extended/client/functions.lua +++ b/[core]/es_extended/client/functions.lua @@ -196,6 +196,7 @@ function ESX.UI.Menu.Open(type, namespace, name, data, submit, cancel, change, c menu.type = type menu.namespace = namespace + menu.resourceName = (GetInvokingResource() or "Unknown") menu.name = name menu.data = data menu.submit = submit @@ -1346,7 +1347,7 @@ end) AddEventHandler('onResourceStop', function(resourceName) for i = 1, #ESX.UI.Menu.Opened, 1 do if ESX.UI.Menu.Opened[i] then - if ESX.UI.Menu.Opened[i].namespace == resourceName then + if ESX.UI.Menu.Opened[i].resourceName == resourceName or ESX.UI.Menu.Opened[i].namespace == resourceName then ESX.UI.Menu.Opened[i].close() ESX.UI.Menu.Opened[i] = nil end