First of all, to those who have given advice and help in the past, again, many, many thank yous!
Just for the record, I inherited SmartRes after the previous devs stepped away from the project, so I am confessing to not entirely understanding their code. I follow it however.
Anyway, I decided to perform some "best practices" on the options file. The original code is thus: http://pastey.net/106962
I changed lines 390-399 to http://pastey.net/106963
to do away with the "this" global, which I understand has depreciated. Now, when I log in, I get errors on lines 83 & 429, and the anchor, when I activate it, gets stuck to the mouse, and only the command /sr anchor turns it off. It is supposed to stop moving and stay in place after releasing the mouse button.
I get what the code is doing (getting new position and updating the variables) but not why ... *sigh* Any suggestions would be helpful, thank you all.
Meaning, I should change all the "this"s to "self"s? I hope the "end" is in the correct place, then.
Yes, any references to "this" can be replaced with self as long as there is a reference to "self" (all tab:func() functions have a reference to "self"); however, if the table is not a reference to the frame, you might run into problems, but this can be solved by changing the call from tab:func(...) to tab.func(frame, ...).
Edit: This is a general example that has nothing to do with your code. :)
the depreciated method was the use of the Global "this", as in _G.this Not the use of the word "this" as a variable name.
In your script handlers define them like so:
f:SetScript("OnDragStart", function([B]this[/B]) this:StartMoving() end )
f:SetScript("OnDragStop", function([B]this[/B])
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)
the handler scripts now pass the frame that the function is being acted upon as the first arg. The Depreciated method is calling the _G.this inside said function to access the frame being acted upon. In reality it dosn't matter what you call that variable, as long as it's defined. People like using "self". Personaly i like using "this" or "frame" when doing frame scripts.
Ah, thank you all for the posts, it becomes clearer now... _G, right... I've seen that in older addons. Good to know that's what needs to be replaced with "good practices."
One final question: why are we changing it, anyway? I mean, what was wrong with using that global?
In Lua, globals are very slightly slower to use than locals. The difference is usually not worth bothering with, but for functions which could be called often, it builds up. Locals include parameters, and the implicit 'self' parameter makes using it instead of a global very easy.
Just for the record, I inherited SmartRes after the previous devs stepped away from the project, so I am confessing to not entirely understanding their code. I follow it however.
Anyway, I decided to perform some "best practices" on the options file. The original code is thus:
http://pastey.net/106962
I changed lines 390-399 to
http://pastey.net/106963
to do away with the "this" global, which I understand has depreciated. Now, when I log in, I get errors on lines 83 & 429, and the anchor, when I activate it, gets stuck to the mouse, and only the command /sr anchor turns it off. It is supposed to stop moving and stay in place after releasing the mouse button.
I get what the code is doing (getting new position and updating the variables) but not why ... *sigh* Any suggestions would be helpful, thank you all.
Yes, any references to "this" can be replaced with self as long as there is a reference to "self" (all tab:func() functions have a reference to "self"); however, if the table is not a reference to the frame, you might run into problems, but this can be solved by changing the call from tab:func(...) to tab.func(frame, ...).
Edit: This is a general example that has nothing to do with your code. :)
In your script handlers define them like so:
the handler scripts now pass the frame that the function is being acted upon as the first arg. The Depreciated method is calling the _G.this inside said function to access the frame being acted upon. In reality it dosn't matter what you call that variable, as long as it's defined. People like using "self". Personaly i like using "this" or "frame" when doing frame scripts.
One final question: why are we changing it, anyway? I mean, what was wrong with using that global?
table:Function() <-- that is implicit self param.
framescript(frame, button) <-- not implicit self param.
:) other than that, farmbuyer is right