Thread Tools Display Modes
04-06-13, 05:09 AM   #1
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Function questions

So i have a function like that:

Code:
function PVPSound_UpdateSoundEffectEngine(self, elapsed)

end

PVPSoundEffectSoundEngineFrame:SetScript("OnUpdate", PVPSound_UpdateSoundEffectEngine)
If i change the function to:

local addon, ns = ...
local PVPSound = { }
ns.PVPSound = PVPSound

Code:
function PVPSound:UpdateSoundEffectEngine(self, elapsed)

end

PVPSoundEffectSoundEngineFrame:SetScript("OnUpdate", PVPSound:UpdateSoundEffectEngine)
Then why cant i make the function local?
Also why cant call the function like that with the SetScript?
  Reply With Quote
04-06-13, 06:21 AM   #2
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Just some code, maybe it helps:
Code:
local test = {} -- local example table
function test:Hello(word) print(word) end -- same as below
function test.Hello(self, word) print(word) end -- same as above
test:Hello("World") -- same as below
test.Hello(test, "World") -- same as above
These are all local because "test" is a local table.

If you want a local function you do local function onupdate(self, elapsed) ... end and now you can SetScript directly to "onupdate", and it is a local function.

The benefit of keeping everything to your "ns" for example is that all the files can access those functions trough the namespace.
__________________
Profile: Curse | Wowhead
  Reply With Quote
04-06-13, 06:57 AM   #3
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
Originally Posted by Resike View Post
[...]
Then why cant i make the function local?
Also why cant call the function like that with the SetScript?
Because your syntax is wrong. You can not set a script handler using the syntactic colon operator. SetScript/HookScript expect a reference to a function. Using the colon is just a shorter way of passing the table as the first argument:
lua Code:
  1. local someaddon = {a = 10, plus = function(self) self.a = self.a + 5 end}
  2. someaddon:plus(5)
  3. print(someaddon.a) -- prints 15
  4.  
  5. local addonb = {a = 30, plus = someaddon.plus}
  6. addonb:plus(10)
  7. print(addonb.a) -- prints 40
  8.  
  9. someaddon.plus(addonb, 5)
  10. print(someaddon.a) -- prints 20
  11. print(addonb.a) -- prints 40
  Reply With Quote
04-06-13, 07:36 AM   #4
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by ravagernl View Post
Because your syntax is wrong. You can not set a script handler using the syntactic colon operator. SetScript/HookScript expect a reference to a function. Using the colon is just a shorter way of passing the table as the first argument:
lua Code:
  1. local someaddon = {a = 10, plus = function(self) self.a = self.a + 5 end}
  2. someaddon:plus(5)
  3. print(someaddon.a) -- prints 15
  4.  
  5. local addonb = {a = 30, plus = someaddon.plus}
  6. addonb:plus(10)
  7. print(addonb.a) -- prints 40
  8.  
  9. someaddon.plus(addonb, 5)
  10. print(someaddon.a) -- prints 20
  11. print(addonb.a) -- prints 40
And if i do it like that?

Code:
local function OnUpdate(self, elapded)
    PVPSound:UpdateSoundEffectEngine(self, elapsed)
end

function PVPSound:UpdateSoundEffectEngine(self, elapsed)

end

PVPSoundEffectSoundEngineFrame:SetScript("OnUpdate", OnUpdate)
  Reply With Quote
04-06-13, 09:48 AM   #5
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
I guess you could, but why would you?

You're creating a function just to call another function that does all the stuff (I presume). As long as your PVPSound table is local, the UpdateSoundEffectEngine function will also be local and you can set it as the script handler.

Lua Code:
  1. function PVPSound:UpdateSoundEffectEngine(elapsed)
  2. -- stuff goes on here
  3. end
  4.  
  5. PVPSoundEffectSoundEngineFrame:SetScript("OnUpdate", PVPSound.UpdateSoundEffectEngine)
__________________
Oh, the simulated horror!
  Reply With Quote
04-06-13, 09:58 AM   #6
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Ailae View Post
I guess you could, but why would you?

You're creating a function just to call another function that does all the stuff (I presume). As long as your PVPSound table is local, the UpdateSoundEffectEngine function will also be local and you can set it as the script handler.

Lua Code:
  1. function PVPSound:UpdateSoundEffectEngine(elapsed)
  2. -- stuff goes on here
  3. end
  4.  
  5. PVPSoundEffectSoundEngineFrame:SetScript("OnUpdate", PVPSound.UpdateSoundEffectEngine)
Oh i see you can call it with "PVPSound.UpdateSoundEffectEngine" have no clue why did i wanted to call it with "PVPSound:UpdateSoundEffectEngine".

Well the PVPSound table is a local, but its a namespace shared local between my addon's files.
  Reply With Quote
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

WoWInterface » Developer Discussions » Lua/XML Help » Function questions


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off