WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Creating AddOn minimap button using LibDBIcon (https://www.wowinterface.com/forums/showthread.php?t=57681)

StafordDev 11-16-19 07:24 AM

Creating AddOn minimap button using LibDBIcon
 
As a developer of PoisonCharges addon (https://www.curseforge.com/wow/addons/poisoncharges) I wanted to update it to the latest WoW Classic version 1.13.2. But I ran into a number of problems trying to remake a minimap button. I tried to use LibDBIcon but can't make it to work because I never worked with libraries when developing addons for WoW.
What I did:
1) copied following libraries to Interface/Addons/PoisonCharges/Libs folder: AceDB-3.0, CallbackHandler-1.0, LibDataBroker-1.1, LibDBIcon-1.0, LibSharedMedia-3.0, LibStub;
2) tried to load them with following XML code (embeds.xml):
Code:

<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/
..\FrameXML\UI.xsd">

<[email protected]@-->
<Script file="Libs\LibStub\LibStub.lua"/>
<Include file="Libs\CallbackHandler-1.0\CallbackHandler-1.0.xml"/>
<Include file="Libs\AceDB-3.0\AceDB-3.0.xml"/>
<[email protected]@-->
<Script file="Libs\LibDataBroker-1.1\LibDataBroker-1.1.lua"/>
<[email protected]@-->
<Script file="Libs\LibSharedMedia-3.0\LibSharedMedia-3.0.lua"/>
<Script file="Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua"/>
<[email protected]@-->

</Ui>

3) of course added it to .toc file:
Code:

...

PoisonCharges.lua
PoisonCharges.xml

embeds.xml

4) put following code into OnEvent function (which fires only once when the addon is loaded):
Code:

    local LDB = LibStub("LibDataBroker-1.1", true)
    local LDBIcon = LDB and LibStub("LibDBIcon-1.0", true)
    if LDB then
        local PC_MinimapBtn = LDB:NewDataObject("PoisonCharges", {
            type = "launcher",
                        text = "PoisonCharges",
            icon = "Interface\\AddOns\\PoisonCharges\\media\\PoisonCharges_Icon",
            OnClick = function(_, button)
                if button == "LeftButton" then PC_SettingsToggle() end
            end,
            OnTooltipShow = function(tt)
                tt:AddLine("PoisonCharges")
                tt:AddLine("|cffffff00Click|r to open the PoisonCharges settings window.")
            end,
        })
        if LDBIcon then
            LDBIcon:Register("PoisonCharges", PC_MinimapBtn, PC_MinimapPos) -- PC_MinimapPos is a SavedVariable which is set to 90 as default
        end
    end

5) and this is the error I get:
Code:

Message: ...s\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:314: attempt to index local 'db' (a number value)
Time: Sat Nov 16 16:14:49 2019
Count: 1
Stack: ...s\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:314: attempt to index local 'db' (a number value)
...s\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:314: in function `Register'
Interface\AddOns\PoisonCharges\PoisonCharges.lua:52: in function `PoisonCharges_OnEvent'
[string "*:OnEvent"]:1: in function <[string "*:OnEvent"]:1>

Locals: self = <table> {
 Register = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:311
 callbacks = <table> {
 }
 Unlock = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:333
 Lock = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:321
 ShowOnEnter = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:409
 SetButtonToPosition = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:442
 SetButtonRadius = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:433
 UnregisterAllCallbacks = <function> defined @Interface\AddOns\PoisonCharges\Libs\CallbackHandler-1.0\CallbackHandler-1.0.lua:174
 notCreated = <table> {
 }
 radius = 5
 GetMinimapButton = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:384
 IsRegistered = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:359
 tooltip = LibDBIconTooltip {
 }
 RegisterCallback = <function> defined @Interface\AddOns\PoisonCharges\Libs\CallbackHandler-1.0\CallbackHandler-1.0.lua:90
 Show = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:350
 GetButtonList = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:425
 UnregisterCallback = <function> defined @Interface\AddOns\PoisonCharges\Libs\CallbackHandler-1.0\CallbackHandler-1.0.lua:153
 Refresh = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:363
 objects = <table> {
 }
 callbackRegistered = true
 Hide = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:345
 IconCallback = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:25
}
name = "PoisonCharges"
object = <table> {
}
db = 359.542840
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = "attempt to index local 'db' (a number value)"
lib = <table> {
 Register = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:311
 callbacks = <table> {
 }
 Unlock = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:333
 Lock = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:321
 ShowOnEnter = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:409
 SetButtonToPosition = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:442
 SetButtonRadius = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:433
 UnregisterAllCallbacks = <function> defined @Interface\AddOns\PoisonCharges\Libs\CallbackHandler-1.0\CallbackHandler-1.0.lua:174
 notCreated = <table> {
 }
 radius = 5
 GetMinimapButton = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:384
 IsRegistered = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:359
 tooltip = LibDBIconTooltip {
 }
 RegisterCallback = <function> defined @Interface\AddOns\PoisonCharges\Libs\CallbackHandler-1.0\CallbackHandler-1.0.lua:90
 Show = <function> defined @Interface\AddOns\PoisonCharges\Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua:350
 GetButtonList = <fun

Thus I'm asking for help: how to make a working movable minimap button for your addon?

StafordDev 11-16-19 08:59 AM

For anyone wondering: if I replace
Code:

LDBIcon:Register("PoisonCharges", PC_MinimapBtn, PC_MinimapPos)
with
Code:

LDBIcon:Register("PoisonCharges", PC_MinimapBtn, self.PC_MinimapPos)
it starts working. Now the second problem: I can move the button, but how I can save the new position?

Rilgamon 11-16-19 10:59 AM

The third value is your storage table. Simply use a table of your savedvariables.

StafordDev 11-16-19 11:58 AM

As I mentioned in the first post, third parameter - PC_MinimapPos - is already a SavedVariable.

Rilgamon 11-16-19 12:20 PM

And the default value of this is a table?

Edit: The important part of your error is this


Code:

name = "PoisonCharges"
object = <table> {
}
db = 359.542840
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = "attempt to index local 'db' (a number value)"

The first two values look ok. The third should be a table but looks like you passed the value of table['minimapPos'] from a previous try or sth.

StafordDev 11-16-19 01:48 PM

The error described in the first post has already been fixed. And yes, PC_MinimapPos is already a table even in defaults. Initialization of variables:
Code:

        -- initialize variables if this is the first time this addon loaded
        if PC_State == nil then
                PC_State = 1
        end
        if PC_MH == nil then
                PC_MH = "Not selected"
        end
        if PC_OH == nil then
                PC_OH = "Not selected"
        end
        if PC_MinimapPos == nil then
                PC_MinimapPos = { 90 }
        end

And here are the contents of PoisonCharges.lua in SavedVariables with default values as a proof:
Code:

PC_State = 1
PC_MH = "Not selected"
PC_OH = "Not selected"
PC_MinimapPos = {
        90, -- [1]
}

There're no errors during gameplay, the addon works perfectly but if I move minimap button the new position is not saved.

Rilgamon 11-16-19 01:55 PM

The table should look like this.
Code:

{
        ["minimapPos"] = 206.3376812873,
        ["hide"] = false,
}


StafordDev 11-16-19 02:03 PM

Now I did as you said and also replaced
Code:

LDBIcon:Register("PoisonCharges", PC_MinimapBtn, self.PC_MinimapPos)
with
Code:

LDBIcon:Register("PoisonCharges", PC_MinimapBtn, PC_MinimapPos)
And now it works perfectly. Thanks for your help!


All times are GMT -6. The time now is 06:26 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI