From 5aa50f5ac7867491a8929fd0f93e437a4450d268 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Wed, 9 Jun 2021 12:37:33 +1000 Subject: [PATCH 1/2] improvement(client/main): Minor optimisation - use the esx_status:onTick event The event was added to esx_status late 2018 but never utilised anywhere; using it here drops ms to 0.0 at all times. #### Note: This event can and should also be used in various HUDs to improve performance. Many of them use 500ms threaded loops, despite status only updating every 1s by default. They also use individual event callbacks to retrieve each status value, while each value is already being sent in the event. --- client/main.lua | 51 ++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/client/main.lua b/client/main.lua index 0a0aa8cb..2d142f92 100644 --- a/client/main.lua +++ b/client/main.lua @@ -51,39 +51,30 @@ AddEventHandler('esx_status:loaded', function(status) status.remove(75) end) - Citizen.CreateThread(function() - while true do - Citizen.Wait(1000) +end) - local playerPed = PlayerPedId() - local prevHealth = GetEntityHealth(playerPed) - local health = prevHealth - - TriggerEvent('esx_status:getStatus', 'hunger', function(status) - if status.val == 0 then - if prevHealth <= 150 then - health = health - 5 - else - health = health - 1 - end - end - end) - - TriggerEvent('esx_status:getStatus', 'thirst', function(status) - if status.val == 0 then - if prevHealth <= 150 then - health = health - 5 - else - health = health - 1 - end - end - end) - - if health ~= prevHealth then - SetEntityHealth(playerPed, health) +AddEventHandler('esx_status:onTick', function(data) + local playerPed = PlayerPedId() + local prevHealth = GetEntityHealth(playerPed) + local health = prevHealth + + for i=1, #data do + if data[i].name == 'hunger' and data[i].percent == 0 then + if prevHealth <= 150 then + health = health - 5 + else + health = health - 1 + end + elseif data[i].name == 'thirst' and data[i].percent == 0 then + if prevHealth <= 150 then + health = health - 5 + else + health = health - 1 end end - end) + end + + if health ~= prevHealth then SetEntityHealth(playerPed, health) end end) AddEventHandler('esx_basicneeds:isEating', function(cb) From 0087ace5348b92116bda2fcb66c93d649ea3bc5a Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sun, 13 Jun 2021 09:04:56 +1000 Subject: [PATCH 2/2] refactor(client/main): Use pairs loop --- client/main.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/main.lua b/client/main.lua index 2d142f92..07502a5c 100644 --- a/client/main.lua +++ b/client/main.lua @@ -58,14 +58,14 @@ AddEventHandler('esx_status:onTick', function(data) local prevHealth = GetEntityHealth(playerPed) local health = prevHealth - for i=1, #data do - if data[i].name == 'hunger' and data[i].percent == 0 then + for k, v in pairs(data) do + if v.name == 'hunger' and v.percent == 0 then if prevHealth <= 150 then health = health - 5 else health = health - 1 end - elseif data[i].name == 'thirst' and data[i].percent == 0 then + elseif v.name == 'thirst' and v.percent == 0 then if prevHealth <= 150 then health = health - 5 else