ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   General Authoring Discussion (https://www.esoui.com/forums/forumdisplay.php?f=174)
-   -   First AddOn problems (https://www.esoui.com/forums/showthread.php?t=7267)

clawe 08/04/17 02:57 AM

First AddOn problems
 
Hello. I'm writing my first AddOn for my personal use.
The idea is pretty simple. I want to automatically withdraw all gold from bank.

Inspired by few other banking addons I've wrote some code.
At first it worked but after some changes I cant get it to work.
I was so stupid that forgot to backup working version.

If you could help me it would be great...

Code:

local addon.name = "BankAllGoldWithdraw"
local GoldIcon = "|t70%:70%:EsoUI/Art/currency/currency_gold.dds|t"

local function withdrawGold()

        local currMoney = GetBankedCurrencyAmount(CURT_MONEY)
       
        local textGold = ("|cFFFFFF" .. currMoney .. "|r" .. GoldIcon)
       
        local wd = "Withdraw: "
               
        if (currMoney > 0) then
                WithdrawCurrencyFromBank(CURT_MONEY, currMoney)
                d(wd .. textGold)
        end
       
end

local function onAddonLoaded(event, addOnName)

        if addOnName == addon.name then
                EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_OPEN_BANK, withdrawGold)
                EVENT_MANAGER:UnregisterForEvent(addon.name, EVENT_ADD_ON_LOADED)
        end
       
end

EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_ADD_ON_LOADED, onAddonLoaded)


Ayantir 08/04/17 03:11 AM

Lua Code:
  1. local addon.name = "BankAllGoldWithdraw"

addon.name cannot be declared. it's addon you should declare as a table and then subtable name.

like

Lua Code:
  1. local addon = [}
  2. addon.name = "BankAllGoldWithdraw"

After a table for 1 variable, is not really good.

just use
Lua Code:
  1. local ADDON_NAME = "BankAllGoldWithdraw"
and it'll be okay.


Lua Code:
  1. local GoldIcon = "|t70%:70%:EsoUI/Art/currency/currency_gold.dds|t"
you should avoid declaring variables which are only used in a block (here your function withdrawGold() outside of your function, especially if it don't require to be outside of it.

A little convention created by zos is to capitalize function names and lowercase first letter of variable names.

Also, please avoid usage of perenthesis if they're not useful. In Lua they are only needed for operator priority changes.

Lua Code:
  1. if (currMoney > 0) then
Lua Code:
  1. if currMoney > 0 then



so I would write your code :

Lua Code:
  1. local ADDON_NAME = "BankAllGoldWithdraw"
  2.  
  3. local function WithdrawGold()
  4.  
  5.     local currMoney = GetBankedCurrencyAmount(CURT_MONEY)
  6.     local goldIcon = "|t70%:70%:EsoUI/Art/currency/currency_gold.dds|t"
  7.  
  8.     local textGold = ("|cFFFFFF" .. currMoney .. "|r" .. goldIcon)
  9.    
  10.     local wd = "Withdraw: "
  11.        
  12.     if currMoney > 0 then
  13.         WithdrawCurrencyFromBank(CURT_MONEY, currMoney)
  14.         d(wd .. textGold)
  15.     end
  16.    
  17. end
  18.  
  19. local function OnAddonLoaded(_, addOnName)
  20.  
  21.     if addOnName == ADDON_NAME then
  22.         EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_OPEN_BANK, WithdrawGold)
  23.         EVENT_MANAGER:UnregisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED)
  24.     end
  25.    
  26. end

Scootworks 08/04/17 03:22 AM

the problem he has is in general: he will withdraw the whole money from the bank. i think that's not he's idea. so you need to withdraw a difference from your bag to the value that you want to keep.

MONEY_YOU_WANT_TO_KEEP_IN_YOUR_BAG = 15000
CURRENT_MONEY_IN_YOUR_BAG = 3000

WITHDRAW_VALUE = MONEY_YOU_WANT_TO_KEEP_IN_YOUR_BAG - CURRENT_MONEY_IN_YOUR_BAG

clawe 08/05/17 12:57 PM

That was and idea. To withdraw all gold.

Thanks Ayantir for all suggestions.
Unfortunately addon still doesn't work.

It looks like function does not fire or event is not registered.

Dolgubon 08/05/17 02:16 PM

What is the error? Looking at the error can be helpful, because it tells you what type of thing is happening, and it can also point you to the location where it is happening.

Is there no error at all? It's possible your functions aren't running. Peppering d() throughout your addon can help you determine what is not running.

Baertram 08/05/17 03:25 PM

What Ayantir has stripped in his code was the addon loaded event lines at the bottom.
Did you put them into your addon again so it is "starting" at all?

Lua Code:
  1. EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED, onAddonLoaded)

clawe 08/06/17 01:21 AM

There is no error displayed, addon just doesn't work.

I've noticed that missing line and added it myself.

No luck so far. Strange.

sirinsidiator 08/06/17 02:02 AM

Try to put error("hello world") into the first line of your lua file. If that doesn't show ingame, you have a problem with your basic addon structure, otherwise try to put it into the loaded event next and see what happens, etc.
If that doesn't help either, you should upload your addon somewhere and link it here, so we can see the whole thing and won't have to guess at the problem.


All times are GMT -6. The time now is 09:07 AM.

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