WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Adding new script to pet journal buttons (https://www.wowinterface.com/forums/showthread.php?t=58791)

Walkerbo 06-10-21 09:54 PM

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:
  1. PetJournalListScrollFrameButton1:SetScript(
  2.     "OnClick",
  3.     function()
  4.         print("PetJournalListScrollFrameButton1")
  5.     end
  6. )
  7. PetJournalListScrollFrameButton2:SetScript(
  8.     "OnClick",
  9.     function()
  10.         print("PetJournalListScrollFrameButton2")
  11.     end
  12. )
  13. PetJournalListScrollFrameButton3:SetScript(
  14.     "OnClick",
  15.     function()
  16.         print("PetJournalListScrollFrameButton3")
  17.     end
  18. )
  19. PetJournalListScrollFrameButton4:SetScript(
  20.     "OnClick",
  21.     function()
  22.         print("PetJournalListScrollFrameButton4")
  23.     end
  24. )
  25. -- etc

However, if I try to build a loop to add the script to each button it doesn't work;
Lua Code:
  1. for index = 1, 11 do
  2.     buttonName = ("PetJournalListScrollFrameButton" .. index)
  3.     print(buttonName) -- debug --
  4.     _G[buttonName]:SetScript(
  5.         "OnClick",
  6.         function()
  7.             print(buttonName) -- debug --
  8.         end
  9.     )
  10. end

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?

Fizzlemizz 06-10-21 11:04 PM

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:
  1. local index = 0
  2. while true do
  3.     index = index + 1
  4.     local buttonName = "PetJournalListScrollFrameButton" .. index
  5.     print(buttonName) -- debug --
  6.     local button = _G[buttonName]
  7.     if not button then break end
  8.     button:HookScript("OnClick", function(self)
  9.         print(self:GetName()) -- debug --
  10.     end)
  11. end
Seems to work.

Walkerbo 06-10-21 11:38 PM

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 :)

Fizzlemizz 06-10-21 11:46 PM

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.

Walkerbo 06-11-21 08:44 PM

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.

Fizzlemizz 06-11-21 08:54 PM

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

Walkerbo 06-11-21 10:42 PM

Hi Fizzlemizz

Thanks for the help and explanation, I really do appreciate your aid. :)


All times are GMT -6. The time now is 02:10 AM.

vBulletin © 2021, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI