Hi all,
For WoW Classic, I’m trying to modify an AddOn to show the amount of instances you are saved to. I have been able to work with most of it, except the timer it seems like, that is giving me a constant error.
attempt to perform arithmetic on local 'elapsed' (a nil value)
I am not sure exactly where this goes wrong, as elapsed should still be a value the game predefined, or so my AddOn experience goes (up till WotLK). Was this changed at any stage?
Here is the full LUA code. I am very appreciative of any help as I cannot seem to find the error:
lootandreset_saved = { { "empty", 0, 0, false }, { "empty", 0, 0, false }, { "empty", 0, 0, false }, { "empty", 0, 0, false }, { "empty", 0, 0, false } };
local lootandreset_currentzone = "";
local lr_interval = 10.0;
local lr_elapsed = 0;
local lr_locked = false;
local lr_loot = false;
function LootAndReset_SlashHandler(msg)
local command = string.lower(msg);
if ( ( command == "on" ) or ( command == "resume" ) or ( command == "show" ) ) then
LootAndResetMain:Show();
end
if ( ( command == "off" ) or ( command == "suspend" ) or ( command == "hide" ) ) then
LootAndResetMain:Hide();
end
if ( ( command == "?" ) or ( command == "help" ) or ( command == "-?" ) ) then
LR_MSG("-- Loot&Reset Help --");
LR_MSG("Rightclick the window to reset the instances.");
LR_MSG("Shift-Leftlick to move the window.");
LR_MSG("Ctrl-Leftlick to hide the window.");
LR_MSG("'/lr on' will enable the addon again.");
end
end
function LootAndReset_OnLoad(self)
self:RegisterEvent("VARIABLES_LOADED");
self:RegisterEvent("PLAYER_ENTERING_WORLD");
end
function LR_MSG(astring)
DEFAULT_CHAT_FRAME:AddMessage(astring, 0.9, 0.9, 0.9, 1);
end
function LootAndReset_OnUpdate(self, elapsed)
lr_elapsed= lr_elapsed + elapsed;
if ( lr_elapsed >= lr_interval ) then
--uiupdate of the instance list;
local inside,type = IsInInstance();
if ( inside ) then
if ( lr_loot ) then
if ( type == "party" ) then
local autoloot = GetAutoLootDefault();
if ( autoloot == 1 ) then
else
SetAutoLootDefault(true);
end
elseif ( type == "raid" ) then
local autoloot = GetAutoLootDefault();
if ( autoloot == 1 ) then
SetAutoLootDefault(false);
end
end
end
if ( type == "party" ) then
local number = LootAndReset_CheckOpenInstance();
if ( number > 0 ) then
if ( lr_locked == lootandreset_currentzone ) then
else
LRHeader:Hide();
lr_locked = lootandreset_currentzone;
lootandreset_saved[number][1] = lootandreset_currentzone;
lootandreset_saved[number][4] = true;
local hour, minute = GetGameTime();
lootandreset_saved[number][2] = hour;
lootandreset_saved[number][3] = minute;
getglobal("LRT"..number):Show();
getglobal("LRN"..number):Show();
end
end
end
else
if ( not UnitIsDeadOrGhost("player") ) then
lr_locked = false;
end
end
if ( LootAndReset_SavedIsEmpty() ) then
LootAndReset_HideInstances();
LootAndReset_ResizeWindow();
else
LootAndReset_UpdateInstance(1);
end
lr_elapsed = 0;
--uiupdate of the instances and times, depending
end
end
function LootAndReset_UpdateInstance(i)
i = tonumber(i);
local next = 6;
if ( i > 5 ) then
LootAndReset_ResizeWindow();
return;
else
if ( lootandreset_saved[i][1] ~= "empty" ) then
LRHeader:Hide();
getglobal("LRN"..i):SetText(lootandreset_saved[i][1]);
local chour, cminute = GetGameTime();
local hourdiff = chour-lootandreset_saved[i][2];
local minutediff = cminute-lootandreset_saved[i][3];
local lrtime = 0;
if ( hourdiff == 0 ) then
lrtime = 60-minutediff;
getglobal("LRT"..i):SetText(tostring(lrtime));
getglobal("LRT"..i):SetTextColor(LootAndReset_GetColor(lrtime));
next = i+1;
elseif ( ( hourdiff == -23 ) or ( hourdiff == 1 ) ) then
--if current minutes > recorded, remove, else post
if ( minutediff >= 0 ) then
LootAndReset_RemoveInstance(i);
next = 1;
else
lrtime = -minutediff;
getglobal("LRT"..i):SetText(tostring(lrtime));
getglobal("LRT"..i):SetTextColor(LootAndReset_GetColor(lrtime));
next = i+1;
end
elseif ( ( ( hourdiff > -23 ) and ( hourdiff < 0 ) ) or ( hourdiff > 2 ) ) then
LootAndReset_RemoveInstance(i);
next = 1;
end
else
getglobal("LRN"..i):Hide();
getglobal("LRT"..i):Hide();
end
end
LootAndReset_UpdateInstance(next);
end
function LootAndReset_OnEvent(frame, event)
if ( event == "VARIABLES_LOADED" ) then
LootAndReset_ShowAll();
LootAndReset_ResizeWindow();
SLASH_LootAndReset1 = "/lr";
SlashCmdList["LootAndReset"] = LootAndReset_SlashHandler;
end
if ( event == "PLAYER_ENTERING_WORLD" ) then
lr_elapsed = 7.0;
end
end
function LootAndReset_ResetInstances()
local inside,type = IsInInstance();
if ( inside and ( type == "party" ) ) then
LR_MSG("L&R: Can't reset while being inside an instance!");
else
if ( LootAndReset_SavedIsEmpty() ) then
ResetInstances();
else
ResetInstances();
for i=1, 5, 1 do
if ( lootandreset_saved[i][1] ~= "empty" ) then
if ( lootandreset_saved[i][4] ) then
lootandreset_saved[i][4] = false;
end
end
end
end
end
end
function LootAndReset_CheckOpenInstance()
lootandreset_currentzone = GetRealZoneText();
for i=1, 5, 1 do
if ( lootandreset_saved[i][1] == lootandreset_currentzone ) then
if ( lootandreset_saved[i][4] ) then
return 0;
end
elseif ( lootandreset_saved[i][1] == "empty" ) then
return i;
end
end
return 0;
end
function LootAndReset_SortAfterRemove(i)
if ( LootAndReset_SavedIsEmpty() ) then
return true;
else
for k=i+1, 5, 1 do
lootandreset_saved[k-1] = lootandreset_saved[k];
end
lootandreset_saved[5] = { "empty", 0, 0, false };
return false;
end
end
function LootAndReset_RemoveInstance(i)
lootandreset_saved[i] = { "empty", 0, 0, false };
LootAndReset_SortAfterRemove(i);
local y = LootAndReset_FindEmpty();
getglobal("LRT"..y):Hide();
getglobal("LRN"..y):Hide();
end
function LootAndReset_ResizeWindow()
if ( LootAndReset_SavedIsEmpty() ) then
LootAndResetMain:SetWidth(90);
LootAndResetMain:SetHeight(26);
LRHeader:Show();
else
LRHeader:Hide();
local amount = LootAndReset_FindEmpty();
if ( amount ) then
amount = amount-1;
else
amount = 5;
end
LootAndResetMain:SetHeight((amount*15)+11);
local stringwidth = LootAndReset_GetMaxStringWidth(amount);
LootAndResetMain:SetWidth(stringwidth+42);
end
end
function LootAndReset_SavedIsEmpty()
for i=1, 5, 1 do
if ( lootandreset_saved[i][1] ~= "empty" ) then
return false;
end
end
return true;
end
function LootAndReset_FindEmpty()
for i=1, 5, 1 do
if ( lootandreset_saved[i][1] == "empty" ) then
return i;
end
end
return false;
end
function LootAndReset_ShowAll()
LRT1:Show();
LRT2:Show();
LRT3:Show();
LRT4:Show();
LRT5:Show();
LRN1:Show();
LRN2:Show();
LRN3:Show();
LRN4:Show();
LRN5:Show();
LRN1:SetTextColor(0.9,0.9,0.9,1);
LRN2:SetTextColor(0.9,0.9,0.9,1);
LRN3:SetTextColor(0.9,0.9,0.9,1);
LRN4:SetTextColor(0.9,0.9,0.9,1);
LRN5:SetTextColor(0.9,0.9,0.9,1);
LRHeader:Show();
LRHeader:SetTextColor(0.9,0.9,0.9,1);
LRHeader:SetText("Loot & Reset");
end
function LootAndReset_GetMaxStringWidth(x)
local maxlength = 0;
for i=1,x,1 do
local length = getglobal("LRN"..i):GetStringWidth();
if ( length > maxlength ) then
maxlength = length;
end
end
return maxlength;
end
--adjusted version of blizzards healthbar color-coding, thank you
function LootAndReset_GetColor(lrtime)
local r,g,b;
if ( lrtime < 0 ) or ( lrtime > 60 ) then
return 0.9,0.9,0.9,1;
end
lrtime = lrtime/60;
if( lrtime > 0.5) then
g = (1.0 - lrtime) * 2;
r = 1.0;
else
g = 1.0;
r = lrtime * 2;
end
b = 0;
return r,g,b,1;
end
function LootAndReset_Hide()
LootAndResetMain:Hide();
end
function LootAndReset_HideInstances()
LRT1:Hide();
LRT2:Hide();
LRT3:Hide();
LRT4:Hide();
LRT5:Hide();
LRN1:Hide();
LRN2:Hide();
LRN3:Hide();
LRN4:Hide();
LRN5:Hide();
end