Thread Tools Display Modes
11/25/14, 10:33 AM   #1
SnowmanDK
 
SnowmanDK's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 161
Check if a specific addon is enabled?

Is there a way to check for this?
I ask as I am trying to integrate support for Item Saver in my Bank Manager Revived.
Item Saver uses a global variable which I pull (ItemSaver_IsItemSaved(item.bag, item.slot)),
It returns 1 if the checked item is saved, otherwise nil.
I made that work fine, but when Item Saver is not enabled in AddOns, then I get a "function expected instead of nil" error.

So... I guess I need to make sure Item Saver is enabled in AddOns before even running this check.
Can this be done?
  Reply With Quote
11/25/14, 10:44 AM   #2
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by SnowmanDK View Post
Is there a way to check for this?
I ask as I am trying to integrate support for Item Saver in my Bank Manager Revived.
Item Saver uses a global variable which I pull (ItemSaver_IsItemSaved(item.bag, item.slot)),
It returns 1 if the checked item is saved, otherwise nil.
I made that work fine, but when Item Saver is not enabled in AddOns, then I get a "function expected instead of nil" error.

So... I guess I need to make sure Item Saver is enabled in AddOns before even running this check.
Can this be done?
It can be easily done. At first check if global function exists and if it does then use it.

Lua Code:
  1. if ItemSaver_IsItemSaved and ItemSaver_IsItemSaved(item.bag, item.slot) then
  2.     --your code here
  3. end

This is a function which I use in Dustman:
lua Code:
  1. local function IsItemProtected(bagId, slotId)
  2.    --pets
  3.    local iconTexture = GetItemInfo(bagId, slotId)
  4.    if iconTexture:lower():find("icons/pet_") then
  5.       return true
  6.    end
  7.  
  8.    --Item Saver support
  9.    if ItemSaver_IsItemSaved and ItemSaver_IsItemSaved(bagId, slotId) then
  10.       return true
  11.    end
  12.  
  13.    --FCO ItemSaver support
  14.    if FCOIsMarked and FCOIsMarked(GetItemInstanceId(bagId, slotId), -1) then
  15.       return true
  16.    end
  17.  
  18.    return false
  19. end

Last edited by Garkin : 11/25/14 at 10:46 AM.
  Reply With Quote
11/25/14, 10:45 AM   #3
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,578
you probably can check if the checkbox is checked in the addon panel, but I think it would be simpler to check if the function exists before calling it.
Code:
if(ItemSaver_IsItemSaved ~= nil) then ItemSaver_IsItemSaved(item.bag, item.slot) end
  Reply With Quote
11/25/14, 12:45 PM   #4
SnowmanDK
 
SnowmanDK's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 161
@sirinsidiator: I tried that solution and it also threw the same error.
@Garkin: Your solution worked (as always).

Thanks to both of you for the suggestions
  Reply With Quote
11/26/14, 06:20 AM   #5
votan
 
votan's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 577
Hi.

In the Wiki is stated, that a callback of EVENT_ADD_ON_LOADED is called for all addons.
This is the reason why we always have to check for our own addon name.
Isn't that exactly what you need?

Cheers
  Reply With Quote
11/26/14, 02:26 PM   #6
Randactyl
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 251
Originally Posted by votan View Post
Hi.

In the Wiki is stated, that a callback of EVENT_ADD_ON_LOADED is called for all addons.
This is the reason why we always have to check for our own addon name.
Isn't that exactly what you need?

Cheers
I like this the best - especially if the addon you are checking for doesn't have any global functions.

Just add the addon to your OptionalDependsOn and then

Lua Code:
  1. if(addOnName == "otherAddon") then
  2.     flag = true
  3. elseif(addOnName == "yourAddon") then
  4.     EVENT_MANAGER:UnregisterForEvent("addonLoadedFunction", EVENT_ADD_ON_LOADED)
  5. else
  6.     return
  7. end

I actually used a version of this for Advanced Filters to create a filter for Item Saver before I changed the way filters could be added.

Last edited by Randactyl : 11/26/14 at 02:28 PM.
  Reply With Quote
11/27/14, 04:40 PM   #7
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
However, consider that Garkin's gets directly to the point: Is the required function there?
If an addon doesn't have any global functions, you're not going to be using one of its functions in your addon.
  Reply With Quote
11/27/14, 07:17 PM   #8
SnowmanDK
 
SnowmanDK's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 161
Just to clarify:
As I wrote in my initial post then I wanted to check if Item Saver was enabled, so my addon can take advantage of it's functions, but not fail if Item Saver is not even installed.

Problem was solved with Garkin's answer.
  Reply With Quote
11/28/14, 03:35 AM   #9
votan
 
votan's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 577
Originally Posted by Sasky View Post
However, consider that Garkin's gets directly to the point: Is the required function there?
If an addon doesn't have any global functions, you're not going to be using one of its functions in your addon.
I knew this discussion will come in the moment I pressed reply.
The question was "Check if a specific addon is enabled?", not "Check if a global function is available?".
There are scenarios, where "Always-check-before-calling" is absolutly sufficient.
And there are scenarios, where such a check should be done once and different functions are registered to be called.
Mainly if used in large, nested loops.
Originally Posted by SnowmanDK View Post
Just to clarify:
As I wrote in my initial post then I wanted to check if Item Saver was enabled, so my addon can take advantage of it's functions, but not fail if Item Saver is not even installed.

Problem was solved with Garkin's answer.
Nethertheless, as all is fine:
Sorry.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Check if a specific addon is enabled?


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