From c45e6d5a7ad279d7643a70bff83c9eb601191e0b Mon Sep 17 00:00:00 2001 From: Meow <30401521+me0wg4ming@users.noreply.github.com> Date: Mon, 23 Mar 2026 11:02:19 +0100 Subject: [PATCH] Detect UnitXP_SP3 once at load instead of per tick Thanks @OldManAlpha for pointing that out. --- libs/librange.lua | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/libs/librange.lua b/libs/librange.lua index e476e5ba..ac7b25eb 100644 --- a/libs/librange.lua +++ b/libs/librange.lua @@ -124,6 +124,16 @@ end) -- Flag to prevent UnitXP calls during logout (crash prevention) local librange_isLoggingOut = false +-- Detect UnitXP_SP3 once at load time to avoid pcall+closure overhead per tick. +-- UnitXP_SP3 exposes UnitXP("distanceBetween", ...) — test it once and cache the result. +local hasUnitXP_SP3 = false +if UnitXP then + local ok, val = pcall(UnitXP, "distanceBetween", "player", "player") + if ok and val then + hasUnitXP_SP3 = true + end +end + librange:Hide() librange:RegisterEvent("ACTIONBAR_SLOT_CHANGED") librange:RegisterEvent("PLAYER_ENTERING_WORLD") @@ -185,14 +195,14 @@ librange:SetScript("OnUpdate", function() local unit = units[this.id] if not UnitIsUnit("target", unit) then -- Try UnitXP_SP3 first (most accurate distance measurement) - local unitxp_success, unitxp_distance = pcall(function() - return UnitXP("distanceBetween", "player", unit) - end) - if unitxp_success and unitxp_distance then - local threshold = (tonumber(C.unitframes.rangecheck_distance) or 40) + 5 - unitdata[unit] = unitxp_distance < threshold and 1 or 0 - this.id = this.id + 1 - return + if hasUnitXP_SP3 then + local unitxp_distance = UnitXP("distanceBetween", "player", unit) + if unitxp_distance then + local threshold = (tonumber(C.unitframes.rangecheck_distance) or 40) + 5 + unitdata[unit] = unitxp_distance < threshold and 1 or 0 + this.id = this.id + 1 + return + end end -- try to read distance via superwow second