View Single Post
04-06-13, 10:50 AM   #7
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Just keep in mind that the word "self" is what ever caller calls the function.

For example:
Code:
local t = {a = 5}
function t:test(a, b, ...) print(self, a, b, ...) end -- simply prints the arguments (joined by space)
t:test("A", "B") -- prints "<object 't'> A B"
t.test("A", "B") -- prints "A B nil"
You see that using ":" suddenly takes the left object of the ":" and passes that, making the "self" in the function refer to it, or if you use a "." then you have to supply what value "self" is assigned.

Also declaring a function, it's important to remember that:
Code:
function t:test() print(self) end -- "self" is the object "t"

function t.test(self) print(self) end -- "self" is what ever you pass into the function (BEWARE, this and the one above are the same! just different way to write them, also this way you can call "self" anything, for instance it could be "kek" and still refer to the proper object.)

function t.test() print(self) end -- "self" is nil
Now getting to my point, if you use something like:
Code:
frame:SetScript("OnUpdate", t.test)
"t.test" is the function, but the "self" you use in that function will in this case reference "frame" and not "t" so keep that in mind if stuff don't work the way you expect!

For instance your function ns:OnUpdate(elapsed) end if you frame:SetScript("OnUpdate", ns.OnUpdate) then when the function is called the "self" object will refer to "frame" and not "ns", so if you try to for instance call another function you should use ns:OtherFunc() and not self:OtherFunc() like you might expect.

Sorry for messy post.
__________________
Profile: Curse | Wowhead
  Reply With Quote