WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   One addon modifying another? (https://www.wowinterface.com/forums/showthread.php?t=58458)

Krainz 12-13-20 10:53 AM

One addon modifying another?
 
I need to make my own addon that modifies a few others so that I don't have to modify their files every time they have an update.

So I went to look into how GW2 UI interacts with the Immersion addon:

Lua Code:
  1. local _, GW = ...
  2.  
  3. local function SkinImmersionAddonFrame()
  4.     local ImmersionFrame = _G.ImmersionFrame
  5.  
  6.     if ImmersionFrame then
  7.  
  8.         ImmersionFrame.TalkBox.BackgroundFrame.TextBackground:SetTexture("Interface/AddOns/GW2_UI/textures/party/manage-group-bg")
  9.  
  10.         ImmersionFrame.TalkBox.Hilite:Hide()
  11.  
  12.         ImmersionFrame.TalkBox.MainFrame.Indicator:SetPoint("TOPRIGHT", -56, -13)
  13.     end
  14. end
  15. GW.SkinImmersionAddonFrame = SkinImmersionAddonFrame

There are two things I don't understand in that code:

1. There is a local variable defined, ImmersionFrame. But what is _G.ImmersionFrame? I know that ImmersionFrame is a frame defined in the Immersion addon's XML files, but why the _G? What does it do?

2. The bit that says 'if ImmersionFrame then' checks if the ImmersionFrame exists, correct? How does the GW2 UI addon guarantee that it's going to overwrite what's originally stated in the Immersion addon?

Rilgamon 12-13-20 11:21 AM

_G represents the global namespace. So it takes the global one and makes it local.
The changes are made to both of them. (they are the same)
Probably you'll see that the toc-file makes sure the other Addon is loaded first.

Fizzlemizz 12-13-20 12:49 PM

As an aside, the _G is not needed in this case because the frame name is added to the global table as a reference when the frame is created (all frames with names are added to the global table by default)

Lua Code:
  1. local ImmersionFrame = _G.ImmersionFrame
  2. --  is the same as:
  3. local ImmersionFrame = ImmersionFrame
  4. -- is the same as:
  5. local ImmersionFrame = _G["ImmersionFrame")
  6. -- is the same as (in ye olde addon speak):
  7. local ImmersionFrame = getglobal("ImmersionFrame")

Code:

if ImmersionFrame then
if the user doesn't have the Immersion addon, the frame won't exist so what comes after the "if" can be ignored.

Krainz 12-13-20 02:28 PM

Quote:

Originally Posted by Fizzlemizz (Post 337903)
As an aside, the _G is not needed in this case because the frame name is added to the global table as a reference when the frame is created (all frames with names are added to the global table by default)

Lua Code:
  1. local ImmersionFrame = _G.ImmersionFrame
  2. --  is the same as:
  3. local ImmersionFrame = ImmersionFrame
  4. -- is the same as:
  5. local ImmersionFrame = _G["ImmersionFrame")
  6. -- is the same as (in ye olde addon speak):
  7. local ImmersionFrame = getglobal("ImmersionFrame")

Code:

if ImmersionFrame then
if the user doesn't have the Immersion addon, the frame won't exist so what comes after the "if" can be ignored.

Oh, thanks for that explanation! It will make it much easier for me to understand what's going on

--

Is it possible to make something happen whenever a local function from another addon happens?

Like, when another addon (such as GW2 UI) makes the local funcion LoadMinimap() happen, is it possible to make my own addon do something?

Fizzlemizz 12-13-20 02:37 PM

Quote:

Originally Posted by Krainz (Post 337904)
Is it possible to make something happen whenever a local function from another addon happens?

Not unless they've placed a reference to the local function (or calling function) in the global table or a call to the function in something globally accessable like a frame script.


All times are GMT -6. The time now is 10:45 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI