Originally Posted by Olvar
What would be more efficient to check if the player is in combat, in your opinion?
1. Calling InCombatLockdown().
2. Registering events PLAYER_REGEN_DISABLED and PLAYER_REGEN_ENABLED and remembering the status in a local bool variable.
|
There is a difference, though the difference is slight.
Firstly, before I say anything, accessing locals is free. However, you probably don't mean local, you probably mean upvalue. But if it were truly a local, it would be a free lookup (and the test is extremely fast).
Calling InCombatLockdown() can be a global call unless you retain a local copy of it, and even then it will require a call. This means this will always be slower.
HOWEVER: The two things you are saying are not the same:
Code:
local isInLockdown = false;
local frame = CreateFrame "FRAME"
frame:RegisterEvent("PLAYER_REGEN_ENABLED");
frame:RegisterEvent("PLAYER_REGEN_DISABLED");
frame:SetScript("OnEvent", function(self, event, ...)
if event == "PLAYER_REGEN_DISABLED"
isInLockdown = true;
elseif event == "PLAYER_REGEN_ENABLED"
isInLockdown = false;
end
end);
This will work for most purposes, but let's say that we had a little change:
Code:
local isInLockdown = false;
local frame = CreateFrame "FRAME"
frame:RegisterEvent("PLAYER_REGEN_ENABLED");
frame:RegisterEvent("PLAYER_REGEN_DISABLED");
frame:SetScript("OnEvent", function(self, event, ...)
if event == "PLAYER_REGEN_DISABLED"
isInLockdown = true;
test();
elseif event == "PLAYER_REGEN_ENABLED"
isInLockdown = false;
test();
end
end);
function test()
assert(isInLockdown == not not InCombatLockdown());
end
You will find assertion failures popping up all over the place. In particular, you go into lockdown at some time shortly after PLAYER_REGEN_DISABLED. At PLAYER_REGEN_DISABLED, you are still not in lockdown. This may be a relevant difference for some addons. Keep this in mind.