Adding new script to pet journal buttons
Hi all
I am trying to add a new script to the pet journal buttons. I can do it manually for each button using this chunk; Lua Code:
However, if I try to build a loop to add the script to each button it doesn't work; Lua Code:
This is the result I am seeing; My questions are; Why does manually setting the script work but not the loop? How do I get the loop to correctly add the script to each pet button? |
Where you're setting the OnClick for each button, you're using a fixed string to print.
print(buttonName) Where is buttonName defined? If it's global and being overwritten before you get to click the that's what it's value will be. If it's local inside a chunk the OnClick handlers can't "see" then it will be nil or??? Lua Code:
|
Hi Fizzlemizz
So I was getting the global name but not setting it to a local so it overwrote itself. Your chunk works perfectly. Thanks for all of your help :) |
If buttonName was global, each click would print the last thing it was set to at the time of the click. If say another addon was using buttonName accidently as a global, it could be set to anything at any time before you clicked.
printing self:GetName() just prints that buttons name so you're not relying on having some possibly dubiously scoped variable set to the right name at the right time. |
Hi Fizzlemizz
So not only was I trying to use a global which could be affected by other addons, using function(self) instead of just function() ensures I get the info from that button itself. Just checking to see if I am really understanding this. |
You are indeed.
The script handler gets passed a parameter (self) (the widget) and you then pass it on to your function by declaring function(self)... end for the script to call. The "self" in the function declaration is more convention, it could be anything Code:
function(frame) print(frame:GetName()) end Code:
function(myWidget) print(myWidget:GetName()) end |
Hi Fizzlemizz
Thanks for the help and explanation, I really do appreciate your aid. :) |
All times are GMT -6. The time now is 12:21 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI