View Full Version : Protected Function Help
Kassie
25-02-2007, 03:14 PM
Hi,
I was hoping someone could give me a little help with the coding of my first add-on, I'm still learning and my skill isn't very high yet.
I'm a little unclear about hooking secure functions and was hoping someone could clear it up a bit for me, as all the explanations and examples I've found are rather confusing for a beginner.
What I'm trying to do in my add-on is to use a single key-binding to cast Hearthstone from UseContainerItem(). I have the following .XML and .LUA files for it.
XML <Bindings>
<Binding name="HEARTHSTONE_CASTHEARTH" header="HEARTHSTONE">
HS_CastStone();
</Binding>
</Bindings>
LUA function HS_FindStone(Hearthstone)
for bag=0,4 do
for slot=1,GetContainerNumSlots(bag) do
if (GetContainerItemLink(bag,slot)) then
if (string.find(GetContainerItemLink(bag,slot), "Hearthstone")) then
UseContainerItem(bag,slot);
end
end
end
end
end
hooksecurefunc("UseContainerItem", HS_CastStone);
I really don't know if I'm getting the whole hooksecurefunc idea right here, and I just get the Protected Function error in game still, so any help as to how to make this work - or even if it's still possible at all - would be much appreciated.
Thanks.
Kassie
25-02-2007, 04:53 PM
Small error in the .LUA I posted before. It should read,
function HS_CastStone(Hearthstone)
for bag=0,4 do
for slot=1,GetContainerNumSlots(bag) do
if (GetContainerItemLink(bag,slot)) then
if (string.find(GetContainerItemLink(bag,slot), "Hearthstone")) then
UseContainerItem(bag,slot);
end
end
end
end
end
hooksecurefunc("UseContainerItem", HS_CastStone);
Although it says at the bottom of page that I can edit my posts, I couldn't find any way to do it, so had to make a reply for this :smiley: .
aspinkorgall
25-02-2007, 08:22 PM
Unless I'm mistaken, the point of hooking a function is so that you can do something on top of the normal actions of that function. Being a protected function, you cannot use UseContainerItem() in your own code.
Furthermore, the idea of securely hooking a function is to allow you to hook functions without tainting them.
You can however hook it, so that you can for example execute MyFunction() each time UseContainerItem() is called by Blizzards code, but you cannot use it as you are attempting to.
Instead, you should use the function SetBindingItem(KEY,ITEM) to bind the use of an item to a key. For example: SetBindingItem("z","Hearthstone").
The best list I've found for these functions is here: http://forums.worldofwarcraft.com/thread.html?topicId=64187212 - the official thread on changes for WoW 2.0.
I hope this is of help to you.
Kassie
27-02-2007, 05:49 AM
Thanks a lot for your reply.
Very helpful, now I understand the usage of protected functions, you explained it very well.
Some of the other resources I have tried using, it seems to me, assume you are already very familiar with .LUA coding and can be hard for a beginner to understand fully.
The SetBindingItem function seems the way to go, but as yet I haven't managed to get that working, still trying with it though, so hopefuly soon (as I said before, still learning, so sometimes it takes a little time for me to get the usage of some functions correct).
Anyway, thanks again for your help, appreciate you taking the time. :smiley:
Tunga
27-02-2007, 12:28 PM
Some of the other resources I have tried using, it seems to me, assume you are already very familiar with .LUA coding and can be hard for a beginner to understand fully.Agree completely. I'd consider myself to be a decent programmer - not spectacular, but decent - in third year of Comp Sci so I should be able to pick up the basics of a language, but I struggled to find anything that just went through the basic syntax so I could play around with it. In the end I learnt most of what I know from butchering other people's mods and seeing what different bits did. I'm not making complex mods at all but a decent tutorial would have helped a lot. The best one I found was just "type this, then this, then this" but didn't really explain what each bit did and people with less experience of other languages would have just been lost in it all. Ah well, I guess messing around yourself is sometimes the best way :) .
aspinkorgall
27-02-2007, 01:11 PM
I completely agree as well, there isn't much of good help out there for a beginner, and I started out exactly the same way - picked the simplest addon I could find, and ripped its guts out to make my own.
The SetBindingItem function seems the way to go, but as yet I haven't managed to get that working, still trying with it though, so hopefuly soon (as I said before, still learning, so sometimes it takes a little time for me to get the usage of some functions correct).
If you continue to have problems, by all means post your code here and we can hopefully help you where you're stuck. As for that particular function, I tested in game using /script SetBindingItem("z", "Hearthstone"). It doesn't allow you to do it in combat (to prevent automation), and presumably the item's name must be localised.
Kassie
02-03-2007, 07:50 AM
Thanks for the help guys.
In the end I decided to use "ClickBinder" to get my keybinding working.
Possibly a little bit overkill as I only have one binding to worry about, but as I wanted people to be able to choose their own key it saved me a lot of extra coding work to parse the chosen key from a slash command and pass it to the SetBinding Item function seperately. With ClickBinder I only had to make one small table and then call it from a simple slash command - much easier. :smiley:
I have the core of my add-on working as I want now, but I'm hoping to add one more function to it before I submit it. I'll try to get that working this weekend if I can, but either way it'll probably be submitted sometime over the weekend. Then I can sit back and wait for people to pick it apart.:grin:
(Actually kind of looking forward to that happening, as it'll help me to see what I could do better and where I possibly went wrong, so I can maybe improve any add-ons I might make in the future.)
It's a fairly simple add-on as my first attempt, but hopefully in the future, as I learn, I can make more and better add-ons for the WoW community. At the very least, it's one more wannabee-author to add to the pot anyway.:laugh:
Regards,
Kassie
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.