1. I seem to recall a few patches ago there was something about depreciating the global this. or this: -- I take it we are supposed to avoid using the "this" global now? I am a bit confused on how to update code  to... what, exactly? If I give this code snippet from SmartRes you can see the this global all over the place, yet it still works as of 3.03.

    function SmartRes:CreateAnchor(text, cRed, cGreen, cBlue)

    local f = CreateFrame("Button", nil, UIParent)

    f:SetWidth(200)

    f:SetHeight(25)



    f.owner = self



    if self.db.profile.barposition.x and self.db.profile.barposition.y and self.db.profile.barposition.anchor and self.db.profile.barposition.point then

    f:ClearAllPoints()

    f:SetPoint(self.db.profile.barposition.point, UIParent, self.db.profile.barposition.anchor, self.db.profile.barposition.x, self.db.profile.barposition.y)

    else

    f:SetPoint("CENTER", UIParent, "CENTER", 0, 50)

    end



    f:SetScript("OnDragStart", function() this:StartMoving() end )

    f:SetScript("OnDragStop",

    function()

    this:StopMovingOrSizing()

    local point, _, anchor, x, y = this:GetPoint()

    this.owner.db.profile.barposition.x = math.floor(x)

    this.owner.db.profile.barposition.y = math.floor(y)

    this.owner.db.profile.barposition.anchor = anchor

    this.owner.db.profile.barposition.point = point

    end)



    f:SetBackdrop({

    --bgFile = "Interface/Tooltips/UI-Tooltip-Background",

    edgeFile = "Interface/Tooltips/UI-Tooltip-Border",

    tile = false, tileSize = 16, edgeSize = 16,

    insets = {left = 5, right = 5, top = 5, bottom = 5}

    })

    f:SetBackdropColor(cRed, cGreen, cBlue, .6)

    f:SetMovable(true)

    f:RegisterForDrag("LeftButton")



    f.Text = f:CreateFontString(nil, "OVERLAY")

    f.Text:SetFontObject(GameFontNormalSmall)

    f.Text:ClearAllPoints()

    f.Text:SetTextColor(1, 1, 1, 1)

    f.Text:SetWidth(200)

    f.Text:SetHeight(25)

    f.Text:SetPoint("TOPLEFT", f, "TOPLEFT")

    f.Text:SetJustifyH("CENTER")

    f.Text:SetJustifyV("MIDDLE")

    f.Text:SetText(text)



    f:Hide()



    return f

    end

    Looking at the code might be awkward out of context, because it is using Candybar.

  2. I seem to recall a few patches ago there was something about depreciating the global this. or this: -- I take it we are supposed to avoid using the "this" global now? I am a bit confused on how to update code  to... what, exactly? If I give this code snippet from SmartRes you can see the this global all over the place, yet it still works as of 3.03.

    function SmartRes:CreateAnchor(text, cRed, cGreen, cBlue)

    local f = CreateFrame("Button", nil, UIParent)

    f:SetWidth(200)

    f:SetHeight(25)



    f.owner = self



    if self.db.profile.barposition.x and self.db.profile.barposition.y and self.db.profile.barposition.anchor and self.db.profile.barposition.point then

    f:ClearAllPoints()

    f:SetPoint(self.db.profile.barposition.point, UIParent, self.db.profile.barposition.anchor, self.db.profile.barposition.x, self.db.profile.barposition.y)

    else

    f:SetPoint("CENTER", UIParent, "CENTER", 0, 50)

    end



    f:SetScript("OnDragStart", function() this:StartMoving() end )

    f:SetScript("OnDragStop",

    function()

    this:StopMovingOrSizing()

    local point, _, anchor, x, y = this:GetPoint()

    this.owner.db.profile.barposition.x = math.floor(x)

    this.owner.db.profile.barposition.y = math.floor(y)

    this.owner.db.profile.barposition.anchor = anchor

    this.owner.db.profile.barposition.point = point

    end)



    f:SetBackdrop({

    --bgFile = "Interface/Tooltips/UI-Tooltip-Background",

    edgeFile = "Interface/Tooltips/UI-Tooltip-Border",

    tile = false, tileSize = 16, edgeSize = 16,

    insets = {left = 5, right = 5, top = 5, bottom = 5}

    })

    f:SetBackdropColor(cRed, cGreen, cBlue, .6)

    f:SetMovable(true)

    f:RegisterForDrag("LeftButton")



    f.Text = f:CreateFontString(nil, "OVERLAY")

    f.Text:SetFontObject(GameFontNormalSmall)

    f.Text:ClearAllPoints()

    f.Text:SetTextColor(1, 1, 1, 1)

    f.Text:SetWidth(200)

    f.Text:SetHeight(25)

    f.Text:SetPoint("TOPLEFT", f, "TOPLEFT")

    f.Text:SetJustifyH("CENTER")

    f.Text:SetJustifyV("MIDDLE")

    f.Text:SetText(text)



    f:Hide()



    return f

    end

    Looking at the code might be awkward out of context, because it is using Candybar.

  3. I seem to recall a few patches ago there was something about depreciating the global this. or this: -- I take it we are supposed to avoid using the "this" global now? I am a bit confused on how to update code  to... what, exactly? If I give this code snippet from SmartRes you can see the this global all over the place, yet it still works as of 3.03.

    function SmartRes:CreateAnchor(text, cRed, cGreen, cBlue)

    local f = CreateFrame("Button", nil, UIParent)

    f:SetWidth(200)

    f:SetHeight(25)



    f.owner = self

    Here, the use of self is fine, because the self it's referring to is the table SmartRes.  No particular reason for noting this other than informational purposes.

    f:SetScript("OnDragStart", function() this:StartMoving() end )

    Here is an example/potential problem.  Here you are using the global variable "this", which you should not be using.  All of the script handlers are passed the frame as the first argument.  Let's name that function and make this more explicit:



    local myOnDragStart = function(self) self:StartMoving() end

    f:SetScript("OnDragStart", myOnDragStart)

    In this case, when Blizzard calls myOnDragStart, it calls it with the frame as the first argument.  Now your function has your own local copy of the frame object so no one else can mess with it.  In the previous mode, something else could change the global this, causing you all sorts of troubles.

    Whenever you are writing a script handler, you should be using the local copy of the arguments that are passed to you.  What arguments are passed are all listed on the individual pages (http://wowprogramming.com/docs/scripts/OnDragStart)

     

  4. Makes sense, as the change would remove any potential conficts, as you said. This is what I did, following your lead:

    local myOnDragStart = function(self) self:StartMoving() end

    f:SetScript("OnDragStart", myOnDragStart)

    local myOnDragStop = function(self) self:StopMovingOrSizing()

    f:SetScript("OnDragStop", myOnDragStop)

    local point, _, anchor, x, y = self:GetPoint()

    self.owner.db.profile.barposition.x = math.floor(x)

    self.owner.db.profile.barposition.y = math.floor(y)

    self.owner.db.profile.barposition.anchor = anchor

    self.owner.db.profile.barposition.point = point

    end

    I still have to test it, just so you know. Eventually I plan on writing an entirely new version and stop using this legacy code that has been patched by several addon authors. In the meantime, SmartRes, at least to me, is a good place to start learning how to write WoW addons.

    Thank you again for the help, the book, and everything else! All of you are amazing.

  5. I suspect that this line here

    local myOnDragStop = function(self) self:StopMovingOrSizing()

    should have the "end" at the end, rather than where it is now. I have this suspicion because in the full addon, given the previously discussed code, the anchor appears, is movable, but can't be turned off except via a slash command.

    We shall see, and I will test as soon as I can.

  6. I want to expand on the sentance above that helped me the most. In case someone else ends up in the position I was. The sentance was: "All of the script handlers are passed the frame as the first argument." In my instance the widget script handler was referenced in the XML file for the addon. I had to add self as an argument in script section of the XML file in addition to the function in the LUA file where the function script is defined to get the value of self to be passed to the function. I bought the book after I saw this thread. It seemed like it would help alot.

  7. Glad it helped you! However, if you look at the date on this thread, then compare to SmartRes, you will find long ago I rewrote (with lots of help) the whole addon into SmartRes2, which probably gives better coding examples.

    It is, naturally, a much better written addon than the original.