View Single Post
04-06-13, 10:59 AM   #8
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Vlad View Post
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.
And which self i'm gonna get if i use it like that?

Code:
function t:test(self)
    print(self)
end
Honestly i don't need the "PVPSound" table for nothing, just to bypass local functions between files.
  Reply With Quote