WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   How to access an addon's function? (https://www.wowinterface.com/forums/showthread.php?t=55937)

siweia 12-27-17 11:14 AM

How to access an addon's function?
 
For example, an addon named "ABC" use title like:
Code:

local A, B, C = unpack(select(2, ...))

A.CreateFrame = function() end
B.CreateColor = function() end
C.CreateName = function() end

If I want to change some of the functions in the table, what should I do in the title to access the addon?
The belowing line seems not working by using the addon name.
Code:

local A, B, C = unpack("ABC")

Kanegasi 12-27-17 11:28 AM

It would be better if you gave the actual code, but I'm going to assume that top line is near the top of the addon's file. When an addon's code loads, it is given a vararg, which is ... and it contains the addon's name and a table that is shared among all files within that addon folder.

When you used the code, you aren't getting the same code that addon is getting. Also, that addon is localizing those functions by putting them in local tables. This means that you can only change them manually in the addon's files.

It is not possible to change those functions without editing that addon's files.

Aftermathhqt 12-27-17 03:18 PM

Quote:

Originally Posted by siweia (Post 326250)
For example, an addon named "ABC" use title like:
Code:

local A, B, C = unpack(select(2, ...))

A.CreateFrame = function() end
B.CreateColor = function() end
C.CreateName = function() end

If I want to change some of the functions in the table, what should I do in the title to access the addon?
The belowing line seems not working by using the addon name.
Code:

local A, B, C = unpack("ABC")

Lua Code:
  1. -- Lib Globals
  2. local _G = _G
  3. local select = select
  4. local unpack = unpack
  5. local tonumber = tonumber
  6. local match = string.match
  7. local floor = math.floor
  8.  
  9. -- Locals
  10. local Resolution = GetCurrentResolution() > 0 and select(GetCurrentResolution(), GetScreenResolutions()) or nil
  11. local Windowed = Display_DisplayModeDropDown:windowedmode()
  12. local Fullscreen = Display_DisplayModeDropDown:fullscreenmode()
  13.  
  14. -- Build the engine
  15. local AddOnName, Engine = ...
  16.  
  17. Engine[1] = {} -- Engine
  18. Engine[2] = {} -- Config
  19. Engine[3] = {} -- Locales
  20.  
  21. function Engine:unpack()
  22.     return self[1], self[2], self[3]
  23. end
  24.  
  25. Engine[1].WindowedMode = Windowed
  26. Engine[1].FullscreenMode = Fullscreen
  27. Engine[1].Resolution = Resolution or (Windowed and GetCVar("gxWindowedResolution")) or GetCVar("gxFullscreenResolution")
  28. Engine[1].ScreenHeight = tonumber(match(Engine[1].Resolution, "%d+x(%d+)"))
  29. Engine[1].ScreenWidth = tonumber(match(Engine[1].Resolution, "(%d+)x+%d"))
  30.  
  31. Engine[1].Mult = 768 / match(Engine[1].Resolution, "%d+x(%d+)") / 0.8
  32. Engine[1].Scale = function(x) return Engine[1].Mult * floor(x / Engine[1].Mult + 0.5) end
  33.  
  34. Engine[1].TexCoords = {0.08, 0.92, 0.08, 0.92}
  35.  
  36. Engine[1].MyName = UnitName("player")
  37. Engine[1].MyClass = select(2, UnitClass("player"))
  38. Engine[1].MyLevel = UnitLevel("player")
  39. Engine[1].MyRealm = GetRealmName()
  40. Engine[1].MyFaction = select(2, UnitFactionGroup("player"))
  41. Engine[1].MyRegion = GetLocale()
  42.  
  43. Engine[1].Version = GetAddOnMetadata(AddOnName, "Version")
  44. Engine[1].VersionNumber = tonumber(Engine[1].Version)
  45. Engine[1].WoWPatch, Engine[1].WoWBuild, Engine[1].WoWPatchReleaseDate, Engine[1].TocVersion = GetBuildInfo()
  46. Engine[1].WoWBuild = tonumber(Engine[1].WoWBuild)
  47.  
  48. _G[AddOnName] = Engine

Heres my code that i use as engine to access all my functions in every file.

Lua Code:
  1. local A, C, L = select(2, ...):unpack()

in every file to access those functions.

siweia 12-28-17 12:16 AM

Quote:

Originally Posted by Game92 (Post 326255)
[highlight="Lua"]-- Lib Globals
local _G = _G
local select = select
local unpack = unpack
local tonumber = tonumber
local match = string.match
local floor = math.floor
...

Code:

local A, C, L = select(2, ...):unpack()
It is only available for the files inside the addon.
For other addon, it should be fine to just "local A, C, L = select(YourAddonName):unpack()", because of the bottom line of your code "_G[AddOnName] = Engine".

So now, for some addon that doesn't have the bottom code as you do, is there any way to access as well?

Kanegasi 12-28-17 02:11 AM

As I stated above, it is not possible. The vararg passed to each file of an addon is through Blizzard’s addon loader and is unique to each addon. You have to edit the addon’s files directly to either change what you want or make what you want to access a global.

Aftermathhqt 12-28-17 05:35 AM

Quote:

Originally Posted by siweia (Post 326266)
Code:

local A, C, L = select(2, ...):unpack()
It is only available for the files inside the addon.
For other addon, it should be fine to just "local A, C, L = select(YourAddonName):unpack()", because of the bottom line of your code "_G[AddOnName] = Engine".

So now, for some addon that doesn't have the bottom code as you do, is there any way to access as well?

AddOnName will use your addon name from your TOC file :)

local A, C, L = unpack(YourAddonName)

siweia 12-28-17 10:30 AM

Quote:

Originally Posted by Game92 (Post 326271)
AddOnName will use your addon name from your TOC file :)

local A, C, L = unpack(YourAddonName)

I know, but the addon I want to access didn't use this "_G[AddOnName] = Engine" as you do.
Currently, the only way I can do is to edit the file inside the addon.

Lolzen 01-06-18 12:14 PM

I had a similar request not too long ago
http://www.wowinterface.com/forums/s...ad.php?t=54765

MunkDev 01-07-18 11:34 AM

Anyone that answers anything but it's up to the addon author to expose their environment is either wrong or misunderstood what you're asking for. If something isn't globally accessible for you to modify, your best bet is probably to ask whoever designed it to make it so.


All times are GMT -6. The time now is 01:48 AM.

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