Thread Tools Display Modes
04-12-11, 01:43 PM   #1
frohanss
A Cyclonian
AddOn Author - Click to view addons
Join Date: Mar 2011
Posts: 40
Show custom frame when having a target?

Im trying to create a panel that toggles on and off based on if i have a target or not. I just can't seem to get it to work.

Im using:
Code:
if (event == PLAYER_TARGET_CHANGED) then
    if (UnitExists("target")) then
	Target_Power:Show()
    else
	Target_Power:Hide()
    end
	end
Baisicly i whant the panel to only be shown when i have a target.


What am i doing wrong?
  Reply With Quote
04-12-11, 03:19 PM   #2
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
I can only suggest to post/pastey your whole code .. don't see anything wrong, assuming that Target_Power is your custom defined frame

you're however missing double quotes for the string "PLAYER_TARGET_CHANGED"

Last edited by Ketho : 04-12-11 at 03:22 PM.
  Reply With Quote
04-12-11, 05:48 PM   #3
frohanss
A Cyclonian
AddOn Author - Click to view addons
Join Date: Mar 2011
Posts: 40
Here is my code:

Code:
if not Stuf then return end
local _, class = UnitClass("player")

local b1 = 5
local b2 = 7
local b3 = 8

local HPh = 60  -- Hight of health bar
local HPw = 220 -- Width of health bar
local PWh = 55	-- Hight of power bar
local PWw = 220 -- Width of power bar

    local backdrop = { 
      bgFile = "Interface\\AddOns\\Centurions\\media\\tga\\background_flat", 
      edgeFile = "Interface\\AddOns\\Centurions\\media\\tga\\glow",
      tile = false,
      tileSize = 32, 
      edgeSize = b3,
      insets = { left = 5, right = 5, top = 5, bottom = 5}
	}

------------------------------------------------------------------------
-- Power Bar / Background Panels
------------------------------------------------------------------------
function CreateHolder1(f, w, h, anchor, parent, parentanchor, x, y)
f:SetWidth(w)
f:SetHeight(h)
f:SetFrameStrata("BACKGROUND",1)
f:SetBackdrop(backdrop)
f:SetBackdropColor(.38,.624,.859,1) --Border 2, Controlls the color
f:SetBackdropBorderColor(0,0,0,0.8) --Border 1, Outer Border
f:SetPoint(anchor, parent, parentanchor, x, y)
-- This creates Border 3
local s = f:CreateTexture(nil, "LOW",nil,-8)
      s:SetPoint("TOPLEFT",f,"TOPLEFT",b2,-b2) -- Numbers set's the size of border
      s:SetPoint("BOTTOMRIGHT",f,"BOTTOMRIGHT",-b2,b2) -- Number set's the size of border
      s:SetTexture(0,0,0,1, true) -- Controlls the color of border in r,g,b,a format
	  s:SetHorizTile(true)
	  s:SetVertTile(true)
      s.t = t	
end

local f = CreateFrame("Frame", "Player_Power", UIParent)
CreateHolder1(f, PWw, PWh, "BOTTOM", UIParent, "BOTTOM", 405, 215)

local f = CreateFrame("Frame", "Target_Power", UIParent)
CreateHolder1(f, PWw, PWh, "BOTTOM", UIParent, "BOTTOM", -405, 215)

if (event == "PLAYER_TARGET_CHANGED") then
    if (UnitExists("target")) then
	Target_Power:Show()
    else
	Target_Power:Hide()
    end
	end
------------------------------------------------------------------------
-- Health Bar 2 / Top Panels 
------------------------------------------------------------------------
function CreateHolder1(f, w, h, anchor, parent, parentanchor, x, y)
f:SetWidth(w)
f:SetHeight(h)
f:SetFrameStrata("MEDIUM",-2)
f:SetBackdrop(backdrop)
f:SetBackdropColor(.38,.624,.859,1) --Border 2, Controlls the color
f:SetBackdropBorderColor(0,0,0,0.8) --Border 1, Outer Border
f:SetPoint(anchor, parent, parentanchor, x, y)
-- This creates Border 3
local s = f:CreateTexture(nil, "LOW",nil,-8)
      s:SetPoint("TOPLEFT",f,"TOPLEFT",b2,-b2) -- Numbers set's the size of border
      s:SetPoint("BOTTOMRIGHT",f,"BOTTOMRIGHT",-b2,b2) -- Number set's the size of border
      s:SetTexture(0,0,0,1, true) -- Controlls the color of border in r,g,b,a format
	  s:SetHorizTile(true)
	  s:SetVertTile(true)
      s.t = t	
 
end

local f = CreateFrame("Frame", "Player_HP2", UIParent)
CreateHolder1(f, HPw, HPh, "BOTTOMLEFT", Player_Power, "BOTTOMLEFT", -15, 15)

local f = CreateFrame("Frame", "Target_HP2", UIParent)
CreateHolder1(f, HPw, HPh, "BOTTOMLEFT", Target_Power, "BOTTOMLEFT", -15, 15)
Basicly custom panels to Stuf Unit frames addon. A strange reason i can't parent panels to that addon. Works perfectly for all my other addons i made panels for. So need to turn panel of when i don't have a target.
  Reply With Quote
04-12-11, 06:46 PM   #4
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
That if-then construct won't do anything just like that .. you need to register your frame to an event
Code:
local function OnEvent(self, event)
	if UnitExists("target") then
		Target_Power:Show()
	else
		Target_Power:Hide()
	end
end

local someFrame = CreateFrame("Frame")
someFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
someFrame:SetScript("OnEvent", OnEvent)
btw, you are reusing/overwriting the "f" variable (which holds the table reference to the frame)
It might be better to use different variable names, if you have multiple frames you want to keep a table reference to
  Reply With Quote
04-12-11, 06:57 PM   #5
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
anyway, I'm really a frame methods/widgets noob, so now is my turn to ask questions

What is the difference between:
Code:
CreateFrame("Frame", "TestFrame")
-- do stuff with global TestFrame
Code:
local tableReference = CreateFrame("Frame")
-- do stuff with local tableReference
Code:
local tableReference = CreateFrame("Frame", "TestFrame")
-- do stuff with local tableReference or global TestFrame
The 3rd one seems superfluous to me, because there already is a global table reference, so isn't it "pointless" to have an additional (local) table reference/pointer to the same table/frame?

Edit: @ Nibelheim
Thanks for giving one of the reasons and an explanation/example

Last edited by Ketho : 04-12-11 at 07:15 PM.
  Reply With Quote
04-12-11, 07:08 PM   #6
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Originally Posted by Ketho View Post
so isn't it "pointless" to have an additional (local) table reference to the same table/frame?
Well, a few reasons. Main reason I do it is so I can have an appropriate frame name and an appropriate variable name.

I.e. I could create an Info Line module, this module consists of a background panel, and info panels.

Now, I could create each frame as so:

Code:
CreateFrame("Frame", "InfoLine_Background")
CreateFrame("Frame", "InfoLine_Durability")
CreateFrame("Frame", "InfoLine_Gold")
CreateFrame("Frame", "InfoLine_Bags")
CreateFrame("Frame", "InfoLine_Clock")
Then reference each of those Frames directly in code whenever I want to do something.

Code:
InfoLine_Durability.text = CreateFontString(etc)
InfoLine_Durability.text:SetFont(font stuff)
InfoLine_Gold.text = CreateFontString(etc)
InfoLine_Gold.text:SetFont(font stuff)
InfoLine_Bags.text = CreateFontString(etc)
InfoLine_Bags.text:SetFont(font stuff)
InfoLine_Clock.text = CreateFontString(etc)
InfoLine_Clock.text:SetFont(font stuff)
Or, I could set up a nice table, and populate it with frames referenced from another table.

Code:
Local ILFrames = {}
for i,v in pairs(Opts.Panels) do
    ILFrames[i] = CreateFrame("Frame", Opts.Panels[i].name)
end
And now I can use a table to reference all my frames.

Code:
for i,v in pairs(Opts.Panels) do
    ILFrames[i].text = CreateFontString(stuff)
    ILFrames[i].text:SetFont(stuff)
end

Last edited by Nibelheim : 04-12-11 at 07:11 PM.
  Reply With Quote
04-12-11, 07:17 PM   #7
frohanss
A Cyclonian
AddOn Author - Click to view addons
Join Date: Mar 2011
Posts: 40
Thank you so much. That solved it.

Im just starting to learn the LUA so a bit nabbing around and much trying out.
Tbh, im a "total" nob with LUA so can't give you a perfect answer.

But i have 2 different "templates". In this example it just the framestrata mainly that is changed. Since im making panels that "partly" overlap.

Im just using the example Sniffles posted to my earlyer question in http://www.wowinterface.com/forums/s...ad.php?t=39182

Thx again

Edited: Thx Nibelheim for your good example.

Last edited by frohanss : 04-12-11 at 07:23 PM. Reason: for not been dead and learning each day :)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Show custom frame when having a target?

Thread Tools
Display Modes

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