WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Button not showing on frame (https://www.wowinterface.com/forums/showthread.php?t=59829)

Codger 03-21-24 01:11 PM

Button not showing on frame
 
I'm a beginner when it comes to addon development...
I created a simple addon with a frame and a button but the button is not showing up on the frame.
I've looked at a few other examples but can't see what I'm doing wrong.
I have bug sack/bug grabber enabled and not seeing any errors.
Any help is much appreciated.

Here is my lua file.

Lua Code:
  1. -- Load the AceGUI and LibDBIcon libraries
  2. local AceGUI = LibStub("AceGUI-3.0")
  3. local LibDBIcon = LibStub("LibDBIcon-1.0")
  4.  
  5. -- Create a new frame
  6. local frame = AceGUI:Create("Frame")
  7. frame:SetTitle("MyAddon Frame")
  8. frame:SetStatusText("Example Status Text")
  9. frame:SetCallback("OnClose", function(widget) AceGUI:Release(widget) end)
  10. frame:SetLayout("Flow")
  11. frame:Hide()
  12.  
  13. -- Create a button
  14. local button = AceGUI:Create("Button")
  15. button:SetText("Click Me!")
  16. button:SetWidth(200)
  17. button:SetCallback("OnClick", function() print("Button Clicked!") end)
  18.  
  19. -- Add the button to the frame
  20. frame:AddChild(button)
  21.  
  22. -- Create a minimap button
  23. local showFrame = false
  24. local icon = LibDBIcon:Register("MyAddon", {
  25.     icon = "Interface\\Icons\\Ability_Marksmanship",
  26.     OnClick = function(self, button)
  27.         if button == "LeftButton" then
  28.             frame:Show()
  29.         elseif button == "RightButton" then
  30.             frame:Hide()
  31.         end
  32.     end,
  33.     OnTooltipShow = function(tooltip)
  34.         tooltip:SetText("MyAddon")
  35.         tooltip:AddLine("Left-click to open", 1, 1, 1)
  36.         tooltip:AddLine("Right-click to close", 1, 1, 1)
  37.     end,
  38. })
  39.  
  40. -- Register the addon
  41. local addonName, addonTable = ...
  42. addonTable.frame = frame

Fizzlemizz 03-21-24 01:41 PM

Code:

frame:Hide()
Will hide the frame until you show it (presumably with the minimap button)

If you start off commenting out the Hide() until you've finished creating the frame and getting it working properly and then worry about the mechanics of showing/hiding it might prove useful.

Codger 03-21-24 01:53 PM

Works better but... question about when to release a widget
 
Thanks for the help. Moving the :hide call fixed the button issue.
I also commented out the line:
--frame:SetCallback("OnClose", function(widget) AceGUI:Release(widget) end)
and now the button shows up with when the frame is opened and closed which makes me wonder when I should be concerned about release the widgets? Do most addons leave the frame open?

Lua Code:
  1. -- Load the AceGUI and LibDBIcon libraries
  2. local AceGUI = LibStub("AceGUI-3.0")
  3. local LibDBIcon = LibStub("LibDBIcon-1.0")
  4.  
  5. -- Create a new frame
  6. local frame = AceGUI:Create("Frame")
  7. frame:SetTitle("MyAddon Frame")
  8. frame:SetStatusText("Example Status Text")
  9. --frame:SetCallback("OnClose", function(widget) AceGUI:Release(widget) end)
  10. frame:SetLayout("Flow")
  11.  
  12. -- Create a button
  13. local button = AceGUI:Create("Button")
  14. button:SetText("Click Me!")
  15. button:SetWidth(200)
  16. button:SetCallback("OnClick", function() print("Button Clicked!") end)
  17.  
  18. -- Add the button to the frame
  19. frame:AddChild(button)
  20.  
  21. --frame:Hide()
  22.  
  23. -- Create a minimap button
  24. local showFrame = false
  25. local icon = LibDBIcon:Register("MyAddon", {
  26.     icon = "Interface\\Icons\\Ability_Marksmanship",
  27.     OnClick = function(self, button)
  28.         if button == "LeftButton" then
  29.             frame:Show()
  30.         elseif button == "RightButton" then
  31.             frame:Hide()
  32.         end
  33.     end,
  34.     OnTooltipShow = function(tooltip)
  35.         tooltip:SetText("MyAddon")
  36.         tooltip:AddLine("Left-click to open", 1, 1, 1)
  37.         tooltip:AddLine("Right-click to close", 1, 1, 1)
  38.     end,
  39. })
  40.  
  41. -- Register the addon
  42. local addonName, addonTable = ...
  43. addonTable.frame = frame

Fizzlemizz 03-21-24 02:02 PM

The game doesn't "destroy" widgets (frames, buttons etc). They remain until you /reload or logout.

I don't use Ace so I can only guess that at what releasing does, possibly some internal pool mechanaism for re-use. That said, just hiding/showing the frame to start with until you get more understanding of how Ace does things won't hurt.

Putting code blocks around code will make it more readable (the # or blue lua buttons above the edit box)

Codger 03-21-24 02:19 PM

Thanks again
 
The code looks so much better.
I really appreciate all the help you provide!!


All times are GMT -6. The time now is 04:15 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI