Thread Tools Display Modes
08-19-15, 11:32 AM   #1
evilbib
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2010
Posts: 30
Question about hooks

Hi,

lets assume I have this setup:
AddOn1\file1.lua
Lua Code:
  1. local addon, ns = ...
  2. local function DoSomething()
  3.     local frame = CreateFrame("Frame")
  4. end

AddOn2\file2.lua
Lua Code:
  1. --ready to hook the function DoSomething() from file1.lua

So is it possible to access the created "frame" in file2.lua?
  Reply With Quote
08-19-15, 11:48 AM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, I'm not sure "hooks" are actually what you're asking about, and you certainly shouldn't create a function in one file in your addon and hook it in another. Hooks are for when you want to modify something in Blizzard code or another addon. If you're writing your own code, just write it the way you want it in the first place.

That said, to share something between files in your addon, just add it to your addon's private table; you've named it "ns" in your code:

AddOn1\file1.lua
Lua Code:
  1. local addon, ns = ...
  2. local frame = CreateFrame("Frame")
  3. ns.frame = frame

AddOn2\file2.lua
Lua Code:
  1. local addon, ns = ...
  2. local frame = ns.frame
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-19-15, 12:36 PM   #3
evilbib
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2010
Posts: 30
Originally Posted by Phanx View Post
Hooks are for when you want to modify something in Blizzard code or another addon.
Thats exactly what I want to do, modify something from another addon (not mine).
AddOn1/file1.lua is all what this addon provides.
  Reply With Quote
08-19-15, 12:39 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, then this is where posting fake code examples gets you in trouble... if the actual code is set up like your example (local "frame" with no name, parent, or distinguishing features created inside a local function) then no, there's no way to access it from another file in another addon. However, it seems unlikely that a real addon would actually do that, so there's probably a way... but without seeing the actual code, there's no way to tell.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-19-15, 02:17 PM   #5
evilbib
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2010
Posts: 30
Originally Posted by Phanx View Post
Well, then this is where posting fake code examples gets you in trouble... if the actual code is set up like your example (local "frame" with no name, parent, or distinguishing features created inside a local function) then no, there's no way to access it from another file in another addon. However, it seems unlikely that a real addon would actually do that, so there's probably a way... but without seeing the actual code, there's no way to tell.
Actually the addon (teksLoot) returns this frame, this is the whole function:
Lua Code:
  1. local function CreateRollFrame()
  2.     local frame = CreateFrame("Frame", nil, UIParent)
  3.     frame:SetWidth(328)
  4.     frame:SetHeight(26)
  5.     frame:SetBackdrop(backdrop)
  6.     frame:SetBackdropColor(0, 0, 0, .9)
  7.     frame:SetScript("OnEvent", OnEvent)
  8.     frame:RegisterEvent("CANCEL_LOOT_ROLL")
  9.     frame:CreateBeautyBorder(11)
  10.     frame:SetBeautyBorderPadding(-1)
  11.     frame:Hide()
  12.  
  13.     local button = CreateFrame("Button", nil, frame)
  14.     button:SetPoint("LEFT", 0, 0)
  15.     button:SetWidth(28)
  16.     button:SetHeight(28)
  17.     --button:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2")
  18.     button:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
  19.     button:GetHighlightTexture():SetBlendMode("ADD")
  20.     button:SetScript("OnEnter", SetItemTip)
  21.     button:SetScript("OnLeave", HideTip2)
  22.     button:SetScript("OnUpdate", ItemOnUpdate)
  23.     button:SetScript("OnClick", LootClick)
  24.     frame.button = button
  25.  
  26.     local buttonborder = CreateFrame("Frame", nil, button)
  27.     buttonborder:SetWidth(32)
  28.     buttonborder:SetHeight(32)
  29.     buttonborder:SetPoint("CENTER", button, "CENTER")
  30.     buttonborder:SetBackdrop(backdrop)
  31.     buttonborder:SetBackdropColor(1, 1, 1, 0)
  32.     buttonborder:CreateBeautyBorder(11)
  33.     buttonborder:SetBeautyBorderPadding(-1)
  34.     frame.buttonborder = buttonborder
  35.  
  36.     local tfade = frame:CreateTexture(nil, "BORDER")
  37.     tfade:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, -4)
  38.     tfade:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -4, 4)
  39.     tfade:SetTexture("Interface\\ChatFrame\\ChatFrameBackground")
  40.     tfade:SetBlendMode("ADD")
  41.     tfade:SetGradientAlpha("VERTICAL", .1, .1, .1, 0, .25, .25, .25, 1)
  42.  
  43.     local status = CreateFrame("StatusBar", nil, frame)
  44.     status:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -4, -4)
  45.     status:SetPoint("BOTTOM", frame, "BOTTOM", 0, 4)
  46.     status:SetPoint("LEFT", frame.button, "RIGHT", -1, 0)
  47.     status:SetScript("OnUpdate", StatusUpdate)
  48.     status:SetFrameLevel(status:GetFrameLevel()-1)
  49.     status:SetStatusBarTexture("Interface\\AddOns\\teksLoot\\media\\statusbar.tga")
  50.     status:SetStatusBarColor(.8, .8, .8, .9)
  51.     status.parent = frame
  52.     frame.status = status
  53.  
  54.     local spark = frame:CreateTexture(nil, "OVERLAY")
  55.     spark:SetWidth(14)
  56.     spark:SetHeight(35)
  57.     spark:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark")
  58.     spark:SetBlendMode("ADD")
  59.     status.spark = spark
  60.  
  61.     local need, needtext = CreateRollButton(frame, "Interface\\Buttons\\UI-GroupLoot-Dice-Up", "Interface\\Buttons\\UI-GroupLoot-Dice-Highlight", "Interface\\Buttons\\UI-GroupLoot-Dice-Down", 1, NEED, "LEFT", frame.button, "RIGHT", 5, -1)
  62.     local greed, greedtext = CreateRollButton(frame, "Interface\\Buttons\\UI-GroupLoot-Coin-Up", "Interface\\Buttons\\UI-GroupLoot-Coin-Highlight", "Interface\\Buttons\\UI-GroupLoot-Coin-Down", 2, GREED, "LEFT", need, "RIGHT", 0, -1)
  63.     local de, detext
  64.     de, detext = CreateRollButton(frame, "Interface\\Buttons\\UI-GroupLoot-DE-Up", "Interface\\Buttons\\UI-GroupLoot-DE-Highlight", "Interface\\Buttons\\UI-GroupLoot-DE-Down", 3, ROLL_DISENCHANT, "LEFT", greed, "RIGHT", 0, -1)
  65.     local pass, passtext = CreateRollButton(frame, "Interface\\Buttons\\UI-GroupLoot-Pass-Up", nil, "Interface\\Buttons\\UI-GroupLoot-Pass-Down", 0, PASS, "LEFT", de or greed, "RIGHT", 0, 2.2)
  66.     frame.needbutt, frame.greedbutt, frame.disenchantbutt = need, greed, de
  67.     frame.need, frame.greed, frame.pass, frame.disenchant = needtext, greedtext, passtext, detext
  68.  
  69.     local bind = frame:CreateFontString()
  70.     bind:SetPoint("LEFT", pass, "RIGHT", 3, 1)
  71.     bind:SetFont("Fonts\\FRIZQT__.TTF", 13, "OUTLINE")
  72.     frame.fsbind = bind
  73.  
  74.     local loot = frame:CreateFontString(nil, "ARTWORK", "SystemFont_Outline")
  75.     loot:SetPoint("LEFT", bind, "RIGHT", 0, .12)
  76.     loot:SetPoint("RIGHT", frame, "RIGHT", -5, 0)
  77.     loot:SetHeight(16)
  78.     loot:SetJustifyH("LEFT")
  79.     frame.fsloot = loot
  80.  
  81.     frame.rolls = {}
  82.  
  83.     return frame
  84. end
  Reply With Quote
08-19-15, 02:41 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Because the function that creates the frame is local, only code within that .lua file can call CreateRollFrame().

Becasue the frame is not given a name, there is no way for code outside that .lua file to access the frame once it has been created unless the fames is parented to another frame that at some stage has been given a name.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 08-19-15 at 02:44 PM.
  Reply With Quote
08-20-15, 05:39 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by pingumania View Post
Actually the addon (teksLoot) ...
In that specific case you're probably better off just modifying the addon directly. It's so small and infrequently updated -- in fact, the last change was made in 2012 -- that porting your changes between versions is trivial. I actually maintain a script that applies small patches to a number of addons after updating, so I don't have to do it by hand.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Question about hooks


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