Problems with elapsed function

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

What’s in your XML file? See also https://wow.gamepedia.com/Porting_addons_to_Classic

Hiya,

Thanks for looking at it! This is what is in my XML file. I’ll take a look at your link shortly!

<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/
..\FrameXML\UI.xsd">
<Script file="LootAndReset.lua"/>
	<Frame name="LootAndResetMain" frameStrata="LOW" toplevel="true" enableMouse="true" movable="true" parent="UIParent" hidden="false">
         <Size>
            <AbsDimension x="100" y="80"/>
        </Size>
        <Anchors>
            <Anchor point="TOPLEFT"/>
        </Anchors>
	<Backdrop bgFile="Interface\TutorialFrame\TutorialFrameBackground" edgeFile="Interface\Tooltips\UI-Tooltip-Border" tile="true">
		<BackgroundInsets>
			<AbsInset left="5" right="4" top="4" bottom="4"/>
		</BackgroundInsets>
		<EdgeSize>
			<AbsValue val="16"/>
		</EdgeSize>
	</Backdrop>		
	<Scripts>
		<OnLoad>
			 self:RegisterForDrag("LeftButton","RightButton");
			 LootAndReset_OnLoad(self);
		</OnLoad>
		<OnEvent>
		         LootAndReset_OnEvent(self, event)
		</OnEvent>
		<OnMouseDown>
			if ( IsShiftKeyDown() and arg1 == "LeftButton" ) then
				LootAndResetMain:StartMoving();
			elseif( IsControlKeyDown() and arg1 == "LeftButton" ) then
				LootAndReset_Hide();
			end
		</OnMouseDown>
		<OnMouseUp>
			LootAndResetMain:StopMovingOrSizing();
			if ( arg1 == "RightButton" ) then
				LootAndReset_ResetInstances();
			end
		</OnMouseUp>
		<OnUpdate>
			LootAndReset_OnUpdate(arg1);
		</OnUpdate>
	</Scripts>
	<Layers>
		<Layer level="OVERLAY">
                 <FontString name="LRT1" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPRIGHT" relativeTo="LootAndResetMain" relativePoint="TOPLEFT">
		    <Offset>
		     <AbsDimension x="25" y="-7"/>
		    </Offset>
		   </Anchor>
	          </Anchors>
	         </FontString>
                 <FontString name="LRT2" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPRIGHT" relativeTo="LRT1" relativePoint="BOTTOMRIGHT">
		    <offset>
		     <AbsDimension x="0" y="-5"/>
		    </offset>
		   </Anchor>
	          </Anchors>
	         </FontString>   
                 <FontString name="LRT3" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPRIGHT" relativeTo="LRT2" relativePoint="BOTTOMRIGHT">
		    <offset>
		     <AbsDimension x="0" y="-5"/>
		    </offset>
		   </Anchor>
	          </Anchors>
	         </FontString>
                 <FontString name="LRT4" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPRIGHT" relativeTo="LRT3" relativePoint="BOTTOMRIGHT">
		    <offset>
		     <AbsDimension x="0" y="-5"/>
		    </offset>
		   </Anchor>
	          </Anchors>
	         </FontString>
                 <FontString name="LRT5" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPRIGHT" relativeTo="LRT4" relativePoint="BOTTOMRIGHT">
		    <offset>
		     <AbsDimension x="0" y="-5"/>
		    </offset>
		   </Anchor>
	          </Anchors>
	         </FontString>	         
                 <FontString name="LRN1" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPLEFT" relativeTo="LRT1" relativePoint="TOPRIGHT">
		    <offset>
		     <AbsDimension x="6" y="0"/>
		    </offset>
		   </Anchor>
	          </Anchors>
	         </FontString>
                 <FontString name="LRN2" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPLEFT" relativeTo="LRT2" relativePoint="TOPRIGHT">
		    <offset>
		     <AbsDimension x="6" y="0"/>
		    </offset>
		   </Anchor>
	          </Anchors>
	         </FontString>
                 <FontString name="LRN3" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPLEFT" relativeTo="LRT3" relativePoint="TOPRIGHT">
		    <offset>
		     <AbsDimension x="6" y="0"/>
		    </offset>
		   </Anchor>
	          </Anchors>
	         </FontString>
                 <FontString name="LRN4" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPLEFT" relativeTo="LRT4" relativePoint="TOPRIGHT">
		    <offset>
		     <AbsDimension x="6" y="0"/>
		    </offset>
		   </Anchor>
	          </Anchors>
	         </FontString>
                 <FontString name="LRN5" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPLEFT" relativeTo="LRT5" relativePoint="TOPRIGHT">
		    <offset>
		     <AbsDimension x="6" y="0"/>
		    </offset>
		   </Anchor>
	          </Anchors>
	         </FontString>
	         <FontString name="LRHeader" inherits="GameFontNormalSmall">
	          <Anchors>
		   <Anchor point="TOPLEFT" relativeTo="LootAndResetMain" relativePoint="TOPLEFT">
		    <Offset>
		     <AbsDimension x="10" y="-7"/>
		    </Offset>
		   </Anchor>
	          </Anchors>
	         </FontString>
	        </Layer>
	</Layers>
	</Frame>
</Ui>
		<OnUpdate>
			LootAndReset_OnUpdate(self, elapsed);
		</OnUpdate>

Already tried that, but then it gives this error:

Interface\AddOns\LootAndReset\LootAndReset.lua:37: attempt to call local 'elapsed' (a number value).

Looking at your link, it also seems stuff like arg1 isn’t live anymore either, but it’s now Var1? This is getting confusing.

That error doesn’t make sense. At least not with your current code. Did you turn it into elapsed() or something because it says you’re trying to call it

You’re right, sorry, I was trying that out at some point.

Okay, now it does work fine! I guess I do need to change the args though, as I cannot move it. /lr off and /lr on work fine though, so it is loaded, I just need to change the method for dragging it.

Do I need to set them myself with custom Vars instead of using global arg1?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.