ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Check if a specific addon is enabled? (https://www.esoui.com/forums/showthread.php?t=2466)

SnowmanDK 11/25/14 10:33 AM

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?

Garkin 11/25/14 10:44 AM

Quote:

Originally Posted by SnowmanDK (Post 13460)
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

sirinsidiator 11/25/14 10:45 AM

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

SnowmanDK 11/25/14 12:45 PM

@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 :banana:

votan 11/26/14 06:20 AM

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

Randactyl 11/26/14 02:26 PM

Quote:

Originally Posted by votan (Post 13482)
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.

Sasky 11/27/14 04:40 PM

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.

SnowmanDK 11/27/14 07:17 PM

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.

votan 11/28/14 03:35 AM

Quote:

Originally Posted by Sasky (Post 13512)
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.
Quote:

Originally Posted by SnowmanDK (Post 13513)
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. :(


All times are GMT -6. The time now is 04:36 PM.

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