Thread Tools Display Modes
08-22-09, 10:16 PM   #1
Subere
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 41
Hooking functions with a function in a table?

So I have something setup similar to the following:

Code:
local addon = {}

function addon:func()
	-- Do stuff.
end

function addon:anotherFunc()
	-- Try to hook something.
	hooksecurefunc('somethingToHook', self:func)
	hooksecurefunc('somethingToHook', addon:func)
	
	-- Only this works.
	hooksecurefunc('somethingToHook', function() self:func() end)
end

As commented out in the code the first 2 ways to hook fail. I'd prefer something like this as it seems cleaner, but I've only gotten the third way to work.

Is there some better way to do this? Or is there just a better way in general I should be setting up an addon?

When I was first messing around with addons and trying to make my own the first one I looked at was set up in a similar way and it seemed nice and clean. It did what I wanted, but I don't really have any knowledge of pros and cons of doing things this way or better ways of doing things.


Sorry for my questions being a bit general but if anyone could educate me a bit on the subject I'd greatly appreciate it. :s Thanks.
  Reply With Quote
08-22-09, 11:08 PM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,934
Hmm, when I do that sort of thing I usually have something as follows:

Code:
local function addon = {};

addon.func1 = function()
-- do stuff
end

addon.func2 = function()
    object.SetScript("OnEnter",addon.func1);
end
I'm not sure how similar it works to your format but that usually works for me.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
08-23-09, 01:08 AM   #3
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
You want to use "self.func" and not "self:func" - the latter is only for function definitions and calls (where "self" is automatically passed as the first parameter), and the former is saying "use this member of self".
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
08-23-09, 12:44 PM   #4
Subere
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 41
Ah thank you a bunch. Coulda sworn I tried that once but guess not :s
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Hooking functions with a function in a table?


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