Thread: GUI's
View Single Post
10-13-05, 04:57 PM   #59
Gello
A Molten Giant
AddOn Author - Click to view addons
Join Date: Jan 2005
Posts: 521
"this." is a table, right? I'm kinda mising it up with "this:"...
Yeah "this" is a table. All frames are tables. Usually (unless you use Ace or any object-oriented stuff), when you see something:SomethingElse(), "something" is a frame, which is a table.

You can add stuff to the frame/table too. It's very common in the default UI to have a frame store its tooltip there.

To backtrack a bit: when an event is sent to your mod, "this" is the frame from which it was sent. The following example doesn't relate to anything but it will hopefully point out exactly what "this" is.

If you have 2 frames in your mod and one has the onload:

<Frame name="MyFrame1">
<Scripts>
<OnLoad>
this:RegisterEvent("BAG_UPDATE")
</OnLoad>
</Scripts>
</Frame>

<Frame name="MyFrame2">
<Scripts>
<OnHide>
this:UnregisterEvent("BAG_UPDATE") <!-- wrong as an example see below -->
</OnHide>
</Scripts>
</Frame>

When the mod loads, this = MyFrame1
When MyFrame2 hides, this = MyFrame2

Note that you registered MyFrame1 to receive BAG_UPDATE. So you would want to use the frame name instead:

<Frame name="MyFrame2">
<Scripts>
<OnHide>
MyFrame1:UnregisterEvent("BAG_UPDATE")
</OnHide>
</Scripts>
</Frame>

As another example, if you have a bunch of buttons in your mod, but don't want to make an _OnClick for each of them. They can all share one function:

<Button name="ButtonTemplate" virtual="true">
<Scripts>
<OnClick>
MyMod_Button_OnClick()
</OnClick>
</Scripts>
</Button>

<Button name="Button1" inherits="ButtonTemplate"/>
<Button name="Button2" inherits="ButtonTemplate"/>
<Button name="Button3" inherits="ButtonTemplate"/>
<Button name="Button4" inherits="ButtonTemplate"/>

then in your lua:

function MyMod_Button_OnClick()
local buttonName = this:GetName()
DEFAULT_CHAT_FRAME:AddMessage(buttonName)
end

When you click Button1-4, this is the name of the frame that called it. (Buttons are frames too) So "this" is the table (or frame) Button1, Button2, Button3, etc.

You can get its name or the name of any named frame with frame:GetName(). So buttonName = this:GetName() will grab the name of the button that was clicked.

btw none of the xml above is fully functional. Just examples kept short.
  Reply With Quote