Quantcast
Adding new script to pet journal buttons - WoWInterface
 
Thread Tools Display Modes
06-10-21, 09:54 PM   #1
Walkerbo
A Flamescale Wyrmkin
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 135
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?
__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote
06-10-21, 11:04 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,424
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.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-10-21 at 11:17 PM.
  Reply With Quote
06-10-21, 11:38 PM   #3
Walkerbo
A Flamescale Wyrmkin
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 135
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
__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote
06-10-21, 11:46 PM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,424
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.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-11-21 at 12:09 AM.
  Reply With Quote
06-11-21, 08:44 PM   #5
Walkerbo
A Flamescale Wyrmkin
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 135
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.
__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote
06-11-21, 08:54 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,424
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
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-11-21 at 09:05 PM.
  Reply With Quote
06-11-21, 10:42 PM   #7
Walkerbo
A Flamescale Wyrmkin
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 135
Hi Fizzlemizz

Thanks for the help and explanation, I really do appreciate your aid.
__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Adding new script to pet journal buttons

Thread Tools
Display Modes

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