Thread Tools Display Modes
12-08-08, 08:13 PM   #1
HolyShield
A Murloc Raider
 
HolyShield's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 5
[help,lua] statusbar from lib... value effecting seperat instances.

Edit: Yep.. a DOH! Moment. I found the problem. I was passing in the same values to the 2nd instance of my statusbar.

I'm working on creating my own version of unit frames (since I have not found one that I have liked) and I'm running into an issue with creating status bars from a lib file. The frame and the text displays properly, however when the value of the status bar is changed on my first instance, the second one also changes... however the reverse is not true. When the second instance changes the first remains the same.

I'm thinking that the 2nd instance of my frame is referencing my 1st instance. I'm not sure why this is happening.
Probably something that will be a "DOH!" moment... but thanks in advance for your time and/or help.


lib tempate for my frame:
Code:
function lib:CreateUnitFrame(name,template)

	if (type(name)) ~= "string" then return; end
	
	--UnitParentFrame
	local frame = getglobal((name.."Frame")) or CreateFrame("Frame",(name.."Frame"), UIParent);
	frame:SetToplevel(true);
	frame:SetMovable(true);
	frame:EnableMouse(true);
	frame:SetClampedToScreen(true);
	frame:SetFrameStrata("LOW");
	frame:SetWidth(128);
	frame:SetHeight(34);
	frame:SetPoint("CENTER");
	
	if (type(template.Backdrop) == "table") then frame:SetBackdrop(template.Backdrop); end
	frame:SetBackdropColor(0,0,0,0.8);
	frame:SetBackdropBorderColor(0,0,0,1);
	
	--UnitName
	frame.unitname = getglobal((name.."UnitName")) or CreateFrame("frame",(name.."unitname"),frame);
	frame.unitname:SetWidth(120);
	frame.unitname:SetHeight(13);
	frame.unitname:SetPoint("CENTER",frame,"TOP");
	
	frame.unitname.text = getglobal((name.."UnitName".."Text")) or frame.unitname:CreateFontString((name.."UnitName".."Text"),"OVERLAY");
	frame.unitname.text:SetWidth(120);
	frame.unitname.text:SetHeight(13);
	frame.unitname.text:SetJustifyH("CENTER");
	frame.unitname.text:SetPoint("CENTER",frame.unitname);
	frame.unitname.text:SetFontObject(GameTooltipTextSmall); 
	frame.unitname.text:SetShadowColor(0,0,0,1);
	frame.unitname.text:SetShadowOffset(1,-1);
	
	--UnitHealth
	frame.unithealth = getglobal((name.."UnitHealth")) or CreateFrame("StatusBar",(name.."UnitHealth"),frame);
	frame.unithealth:SetWidth(120);
	frame.unithealth:SetHeight(13);
	frame.unithealth:SetPoint("CENTER",frame,"CENTER",0,6);
	if(template.StatusBarTexture) then frame.unithealth:SetStatusBarTexture(template.StatusBarTexture); end
	frame.unithealth:SetMinMaxValues(0,100);
	frame.unithealth:SetValue(100);
	frame.unithealth:SetStatusBarColor(0,1,0,0.6);
	
	frame.unithealth.text = getglobal((name.."UnitHealth".."Text")) or frame.unithealth:CreateFontString((name.."UnitHealth".."Text"),"OVERLAY");
	frame.unithealth.text:SetWidth(120);
	frame.unithealth.text:SetHeight(13);
	frame.unithealth.text:SetPoint("CENTER",frame.unithealth,"CENTER");
	frame.unithealth.text:SetJustifyH("CENTER");
	frame.unithealth.text:SetJustifyV("MIDDLE");
	frame.unithealth.text:SetFontObject(GameTooltipTextSmall);
	frame.unithealth.text:SetShadowColor(0,0,0,1);
	frame.unithealth.text:SetShadowOffset(1,-1);
	
	--UnitMana
	frame.unitmana = getglobal((name.."UnitMana")) or CreateFrame("StatusBar",(name.."UnitMana"),frame);
	frame.unitmana:SetWidth(120);
	frame.unitmana:SetHeight(13);
	frame.unitmana:SetPoint("CENTER",frame,"CENTER",0,-6);
	if(template.StatusBarTexture) then frame.unitmana:SetStatusBarTexture(template.StatusBarTexture); end
	frame.unitmana:SetMinMaxValues(0,100);
	frame.unitmana:SetValue(100);
	frame.unitmana:SetStatusBarColor(0,0,1,0.6);
	
	frame.unitmana.text = getglobal((name.."UnitMana".."Text")) or frame.unitmana:CreateFontString((name.."UnitMana".."Text"),"OVERLAY");
	frame.unitmana.text:SetWidth(120);
	frame.unitmana.text:SetHeight(13);
	frame.unitmana.text:SetPoint("CENTER",frame.unitmana,"CENTER");
	frame.unitmana.text:SetJustifyH("CENTER");
	frame.unitmana.text:SetJustifyV("MIDDLE");
	frame.unitmana.text:SetFontObject(GameTooltipTextSmall);
	frame.unitmana.text:SetShadowColor(0,0,0,1);
	frame.unitmana.text:SetShadowOffset(1,-1);
	
	return frame;
end
actual use of the above function:
Code:
function HSTI.CreateStatusString(value, valuemax)
if (type(value) ~= "number") or (type(valuemax) ~= "number") then return; end
	local rtn = "";
	rtn = (value.."/"..valuemax.." ("..format("%.f", value / valuemax * 100).."%)");
	return rtn;
end



HSTI.PlayerName = UnitName("player") or "Unknowen";
function HSTI:GetPlayerData()
	HSTI.PlayerHealth = UnitHealth("player") or 0;
	HSTI.PlayerHealthMax = UnitHealthMax("player") or 1;	
	HSTI.PlayerMana = UnitMana("player") or 0;
	HSTI.PlayerManaMax = UnitManaMax("player") or 1;
end

--PlayerFrame
local playerframe = lib:CreateUnitFrame((HSTI.AddonName.."PlayerFrame"), HSTI.Templates)
if (not playerframe) then return; end
---Display PlayerName
playerframe.unitname.text:SetText(HSTI.PlayerName);

---Register Events
for __,event in pairs ({"VARIABLES_LOADED","PLAYER_ENTERING_WORLD","UNIT_HEALTH","UNIT_MANA"}) do
	playerframe:RegisterEvent(event);
end

---Events
playerframe:SetScript("OnMouseDown",function()
	if (ChatFrameEditBox:IsVisible() and IsShiftKeyDown()) then
	
	elseif (IsControlKeyDown()) then
		playerframe:StartMoving();
	end
end);
playerframe:SetScript("OnMouseUp", function() playerframe:StopMovingOrSizing(); end);
playerframe:SetScript("OnDragStart", function() playerframe:StartMoving(); end);
playerframe:SetScript("OnDragStop", function() playerframe:StopMovingOrSizing(); end);
playerframe:SetScript("OnEvent",function() 
	HSTI:GetPlayerData();
	playerframe.unithealth.text:SetText(HSTI.CreateStatusString(HSTI.PlayerHealth,HSTI.PlayerHealthMax));
	playerframe.unitmana.text:SetText(HSTI.CreateStatusString(HSTI.PlayerMana,HSTI.PlayerManaMax));
	
	playerframe.unithealth:SetValue(HSTI.PlayerHealth / HSTI.PlayerHealthMax * 100);
	playerframe.unitmana:SetValue(HSTI.PlayerMana / HSTI.PlayerManaMax * 100);

end);

--Target Frame
local targetframe = lib:CreateUnitFrame((HSTI.AddonName.."TargetFrame"), HSTI.Templates)
if (not targetframe) then return; end
---Display TargetName

---Register Events
for __,event in pairs ({"UNIT_HEALTH","UNIT_MANA","UNIT_MAXHEALTH","UNIT_MAXMANA","PLAYER_TARGET_CHANGED"}) do
	targetframe:RegisterEvent(event);
end

---Events
targetframe:SetScript("OnMouseDown",function()
	if (IsControlKeyDown()) then
		targetframe:StartMoving();
	end
end);

targetframe:SetScript("OnMouseUp", function() targetframe:StopMovingOrSizing(); end);
targetframe:SetScript("OnDragStart", function() targetframe:StartMoving(); end);
targetframe:SetScript("OnDragStop", function() targetframe:StopMovingOrSizing(); end);
targetframe:SetScript("OnEvent",function() 
	local targetHealth = UnitHealth("target") or 0;
	local targetHealthMax = UnitHealthMax("target") or 1;
	local targetMana = UnitMana("target") or 0;
	local targetManaMax = UnitManaMax("target") or 1;
	
	targetframe.unitname.text:SetText(UnitName("target"));
	
	targetframe.unithealth:SetValue(HSTI.PlayerHealth / HSTI.PlayerHealthMax * 100);
	targetframe.unitmana:SetValue(HSTI.PlayerMana / HSTI.PlayerManaMax * 100);
	
	targetframe.unithealth.text:SetText(HSTI.CreateStatusString(targetHealth, targetHealthMax));
	targetframe.unitmana.text:SetText(HSTI.CreateStatusString(targetMana, targetManaMax));
end);

Last edited by HolyShield : 12-08-08 at 09:25 PM. Reason: Solved my question.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » [help,lua] statusbar from lib... value effecting seperat instances.


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off