Thread Tools Display Modes
12-16-12, 07:40 PM   #1
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
Frame show/hide in different function

In a function I created a frame called US_Frame1, and in this I created a checkbutton that shows or hides the frame. If I write the function in this way the frame is shown/hidden properly

Lua Code:
  1. function SetEnabled(info, newValue)
  2. UnitScanner_Settings["Bars"][self.CurrentBar]["enabled"] = newValue
  3. if (newValue == true) then US_Frame1:Show()
  4. else US_Frame1:Hide()
  5. end
  6. end

but if written in this manner does not work

Lua Code:
  1. function SetEnabled(info, newValue)
  2. UnitScanner_Settings["Bars"][self.CurrentBar]["enabled"] = newValue
  3. local frm = "US_Frame" .. 1
  4. if (newValue == true) then frm:Show()
  5. else frm:Hide()
  6. end
  7. end

If I want show/hide the frame without writing the name (the frame to show/hide changes depending on the situation) how can I do?
  Reply With Quote
12-16-12, 07:48 PM   #2
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 331
Please show your full code.

There are ways to optimize how you'd like to do this, but ultimately you'll want to use a function that passes the frame. Also, you'd definitely want to set that function to local and pass the 'self' frame whatever that may be. For example:

Lua Code:
  1. local function SetEnabled(info, newValue, frame, self)
  2.     UnitScanner_Settings["Bars"][self.CurrentBar]["enabled"] = newValue
  3.     if frame and type(frame) == "table" then
  4.         if (newValue == true) then frame:Show()
  5.     else
  6.         frame:Hide()
  7.     end
  8. end

Alternatively you can use a indexed value on your checkbox button frame to indicate what frame to hide and assign it a general function and index itself.

Lua Code:
  1. local function CheckboxOnClick(self)
  2.     local enabled = self:GetChecked()
  3.     local frame = _G[self.targetFrame] -- when creating the checkbox, you set this to the name of the frame you're wanting to hide.
  4.     UnitScanner_Settings["Bars"][frame:GetName()]["enabled"] = enabled
  5.     if enabled then
  6.         frame:Show()
  7.     else
  8.         frame:Hide()
  9.     end
  10. end

Last edited by suicidalkatt : 12-16-12 at 08:01 PM.
  Reply With Quote
12-16-12, 08:08 PM   #3
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
The problem is that not a single frame that I would like to show or hide, but they are different. The frame that I want to hide is selected from a dropdown menu. The frames are created by this function

http://lua.nopaste.dk/p16108

and are shown/hidden by the function written above
  Reply With Quote
12-16-12, 10:00 PM   #4
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Code:
function SetEnabled(info, newValue)
    UnitScanner_Settings["Bars"][self.CurrentBar]["enabled"] = newValue
    local frm = "US_Frame" .. 1
    if (newValue == true) then
        frm:Show()
    else
        frm:Hide()
    end
end
The reason that doesn't work is "frm" is assigned a string, you need to change it to get the actual object:

Code:
function SetEnabled(info, newValue)
    UnitScanner_Settings["Bars"][self.CurrentBar]["enabled"] = newValue
    local frm = _G["US_Frame" .. 1]
    if (newValue == true) then
        frm:Show()
    else
        frm:Hide()
    end
end
  Reply With Quote
12-24-12, 06:50 AM   #5
Choonstertwo
A Chromatic Dragonspawn
 
Choonstertwo's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 194
On a side note, MoP added a Region:SetShown method, allowing you to replace this:
Code:
if conditions then
	frame:Show()
else
	frame:Hide()
end
With this:
Code:
frame:SetShown(conditions)
Where conditions is some expression like newValue == true.

I'm not sure whether SetShown accepts any true- or false-equivalent values like if statements do or only actual boolean true/false values.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Frame show/hide in different function


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