Thread Tools Display Modes
11-05-10, 03:25 PM   #1
Sideshow
A Flamescale Wyrmkin
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 103
Checking for files and [brackets]

Hello

2 little questions:

- is there a way to check if a file is loaded. Some sort of isFile function
- what's the difference between using myfile = [[myfile]] and myfile = "myfile"



Last edited by Sideshow : 11-05-10 at 03:29 PM.
  Reply With Quote
11-05-10, 05:33 PM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
Originally Posted by Sideshow View Post
Hello

2 little questions:

- is there a way to check if a file is loaded. Some sort of isFile function
- what's the difference between using myfile = [[myfile]] and myfile = "myfile"


"No" to the first.

Difference is first version uses the string verbatim.
You can use [[Interface\AddOns\MyAddon\Sounds\my.mp3]]
as opposed to
"Interface\\AddOns\\MyAddon\\Sounds\\my.mp3" for the second. (had to escape '\')
  Reply With Quote
11-06-10, 06:04 AM   #3
eberkain
A Fallenroot Satyr
Join Date: Nov 2009
Posts: 20
Originally Posted by Dridzt View Post
"No" to the first.

Difference is first version uses the string verbatim.
You can use [[Interface\AddOns\MyAddon\Sounds\my.mp3]]
as opposed to
"Interface\\AddOns\\MyAddon\\Sounds\\my.mp3" for the second. (had to escape '\')
I agree there is no file checking function, but couldn't he declare a unique global variable in a file, and then do an if varname == nil in a different file, wouldn't that serve the same function in most cases?
  Reply With Quote
11-06-10, 08:02 AM   #4
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
Yea I put no in double-quotes because there's tricks you can do to "fake it".

But I got the OPs question to mean checking for a file that's not under your control.
(ie a file of your own addon that might or might not be present but you have control over what it contains if it's actually there)

And wowlua has no generic file io capabilities whatsoever so you cannot check for arbitrary files.
  Reply With Quote
11-06-10, 06:18 PM   #5
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
Well, I'm kinda confused on what file your trying to test for.

If its a file within your addon folder, simply make sure it gets called before the one that needs it in the .toc (or .xml <Script />)

if its a file from another addon, in your .toc add # Dependencies: OtherAddon

and that addon (and its associated files) will be loaded first.


also

Code:
local bool, retVal1, retVal2, reValn = pcall(someFunc, param1, param2, paramn)

if bool then
    --function was a success,
else
    --function threw an error :(
end
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
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

WoWInterface » Developer Discussions » General Authoring Discussion » Checking for files and [brackets]


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