View Single Post
11-07-10, 08:26 AM   #6
Taroven
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 49
Post

Never felt the need myself, but this should work fine.

When an addon is loaded, each file is passed two arguments (accessed via "..."): The addon's name as WoW sees it, and a table. The table is specific to the addon and intended to be its namespace, allowing you to share variables and other info between files without polluting the global namespace (_G).

Knowing that, we can use that namespace to get around the file-checking issue.

Code:
-- Top of every file (change 'fileName' as needed)
local AddonName,ns = ...
ns.loadedFiles = ns.loadedFiles or {}
ns.loadedFiles['fileName'] = true

...

-- Check for the existence (or removal) of files. This only really needs to be in one file, can be folded into an existing OnEvent, and can do whatever you want.
local frame = CreateFrame('Frame',nil,UIParent)
frame:SetScript('OnEvent', function (self,event,addon)
    if addon == AddonName then
        self:UnregisterEvent('ADDON_LOADED')
        if ns.loadedFiles['otherFile'] then
            print('Found a file!')
        end
    end
end)
frame:RegisterEvent('ADDON_LOADED')
You want to check at ADDON_LOADED or your normal init event to be sure that every file is loaded. You can do some pretty cool stuff with this, like calling a certain function for each file or creating dummy variables if a specific file isn't found.

If you're working with libraries and such, you probably don't want to change their code. There are other, less invasive methods of checking for them.

Edit: And yes, you can turn that into an on-demand function instead.
Code:
local function isFile(file)
    return ns.loadedFiles[file]
end
...though that does the same thing as just using if(ns.loadedFiles[file]) directly.
__________________
Former author of EventHorizon Continued and Other Releases.

Last edited by Taroven : 11-07-10 at 09:04 AM.
  Reply With Quote