Thread Tools Display Modes
08-11-08, 09:48 AM   #1
Samahell
A Defias Bandit
Join Date: Aug 2008
Posts: 2
Accessing a widget by its name stored in a variable

Hi all,

I'm looking for a way to access a widget by its name in a string, so I can operate modifications on the name and access the widget's methods.
Something like the Javascript's GetElementById(id) or GetElementByName(name) would be perfect.

Actually, my point is to access sequentially to some methods of several brother (same parent) widgets with similar names, so that the next exemple :
Code:
My_Button_1:SetText(textTab[1]);
My_Button_1:Enable();
My_Button_2:SetText(textTab[2]);
My_Button_2:Enable();
...
could be changed in something like that :

Code:
for x=1, y, 1 do
  My_Button = AccessMyWidgetByName("My_Button_" .. toString(x));
  My_Button:SetText(textTab[x]);
  My_Button:Enable();
end
Thank you.

(Excuse my english, I'm french, so donot hesitate to ask me to reformulate some sentences if I expressed them badly).
  Reply With Quote
08-11-08, 10:30 AM   #2
haste
Featured Artist
 
haste's Avatar
Premium Member
Featured
Join Date: Dec 2005
Posts: 1,027
Just index the global table.
Code:
for x=1, y, 1 do
  local button = _G["My_Button_" .. x];
  button:SetText(textTab[x]);
  button:Enable();
end
  Reply With Quote
08-11-08, 11:51 AM   #3
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Just for interest:

What's the difference between

Code:
local button = _G["My_Button_" .. x];
and

Code:
local button = getglobal("My_Button_" .. x)
?
  Reply With Quote
08-11-08, 02:45 PM   #4
Xolan
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 23
I prefer using getglobal() myself, but I'm guessing that it's slightly inefficient compared to the other, in terms of cpu and memory usage.
  Reply With Quote
08-11-08, 03:30 PM   #5
Samahell
A Defias Bandit
Join Date: Aug 2008
Posts: 2
Ok thanks a lot, this is exactly what I was looking for, I should have thought to this by myself ^^
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Accessing a widget by its name stored in a variable


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