You'll also want to make sure you never call this function more than once, or you will be creating more and more and more frames. Unless you add checks of course.
You'll also want to make sure you never call this function more than once, or you will be creating more and more and more frames. Unless you add checks of course.
Yeah that's why I'm trying to check on the frame and Hide() it first
Yeah that's why I'm trying to check on the frame and Hide() it first
Even when the frame is hidden, the code shown still creates a new frame from scratch. Maybe it should do a 'return' from that check? Depends on what the goal of the function is.
Also, naming frequently used variables the same name in different scopes often leads to code confusion.
You're declaring AyrReadyCheckFrame as a local above the function (upvalue) and then again inside the function and also, implicitly, as a global on the same line inside the function. All programming languages will use the shortest scope possible when referencing variables.
Case in point:
local variable = 5
function doStuff()
print(variable)
local variable = variable + 1
end
doStuff() -- this will print 5 no matter how many times it's called.
local variable = 5
function doStuff()
local variable = variable + 1
print(variable)
end
doStuff() -- this will print 6 no matter how many times it's called.
Edit: There are ways of creating frames without accidentally recreating them.
A method I like to use is:
local addonName = ...
local addonFrame
local function MakeFrame()
addonFrame = _G[addonName] or CreateFrame("Frame", addonName)
end
I'm basically wanting to redraw the frame and its widgets. Should I actually just destroy it (if so how would I do that) or is what I've got good enough?
I imagine my current method will just keep on drawing over and over so when the frame is opened it will actually have many objects one over the top of eachother?
You can't destroy frames and the client is perfectly capable of redrawing the frame by itself. If you want to change the appearance of it (text, textures, etc.), just use their respective functions (FontInstance:SetText(), Texture:SetTexture(), etc.).
I'm basically wanting to redraw the frame and its widgets. Should I actually just destroy it (if so how would I do that) or is what I've got good enough?
I imagine my current method will just keep on drawing over and over so when the frame is opened it will actually have many objects one over the top of eachother?
A frame once created, can be shown or hidden. It is always there, and cannot be destroyed, shown means the user will actually see it, while hidden means the user won't. In both cases, whether shown or hidden, the frame will still respond to registered events.
If you created a 2nd frame and hide the 1st, then all you would be exactly that. If you then create and show a 3rd frame and hide the 2nd, you will now have 2 hidden frames and 1 shown frame. This goes on until you run out of memory as more and more frames get created and WoW crashes. You want to avoid this by using the same frame by showing/hiding it.
If you just want one frame to display to the user, then create it directly outside of any function near the top of your addon, that ensures its available to the rest of your addon. No need to use a function to create the frame unless you actually want to delay the creation of the frame to a later time for a specific reason.
Here is my frame build function
when trying to call AyrReadyCheckFrame:Hide() it cant find the frame and throws the following error;
attempt to index upvalue 'AyrReadyCheckFrame' (a nil value)
I'm assuming that I'm doing something stupid :S any help would be much appreciated
local AyrReadyCheckShown = 0
local AyrReadyCheckFrame
Yeah that's why I'm trying to check on the frame and Hide() it first
In that case, don't redeclare AyrReadyCheckFrame as a new local variable in the function; it will become a separate entity.
Even when the frame is hidden, the code shown still creates a new frame from scratch. Maybe it should do a 'return' from that check? Depends on what the goal of the function is.
Hiding the frame doesn't destroy the frame. The code you have above still continues to create a frame after the check anyway.
You're declaring AyrReadyCheckFrame as a local above the function (upvalue) and then again inside the function and also, implicitly, as a global on the same line inside the function. All programming languages will use the shortest scope possible when referencing variables.
Case in point:
Edit: There are ways of creating frames without accidentally recreating them.
A method I like to use is:
I imagine my current method will just keep on drawing over and over so when the frame is opened it will actually have many objects one over the top of eachother?
A frame once created, can be shown or hidden. It is always there, and cannot be destroyed, shown means the user will actually see it, while hidden means the user won't. In both cases, whether shown or hidden, the frame will still respond to registered events.
If you created a 2nd frame and hide the 1st, then all you would be exactly that. If you then create and show a 3rd frame and hide the 2nd, you will now have 2 hidden frames and 1 shown frame. This goes on until you run out of memory as more and more frames get created and WoW crashes. You want to avoid this by using the same frame by showing/hiding it.
If you just want one frame to display to the user, then create it directly outside of any function near the top of your addon, that ensures its available to the rest of your addon. No need to use a function to create the frame unless you actually want to delay the creation of the frame to a later time for a specific reason.
... or don't know how many frames you'll need.