ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Help with first addon, Lua not initializing? (https://www.esoui.com/forums/showthread.php?t=9632)

xgoku1 03/16/21 05:23 AM

Help with first addon, Lua not initializing?
 
Hey there, I am trying to write a small add-on as a learning project, that counts number of lockpicks left and increments a counter whenever a lockpick-success event occurs.

So far I've written the txt, xml and lua files based on the AddOn scripting guides from the EsoUI wiki, and I can see the GUI (xml) in-game, however it seems that the LUA file does not load as it doesn't have any functionality beyond showing the strings set to labels in the XML.

(Saving the new position of the UI elements to savedvariables does not seem to work either, so the xml is set to where I want it to be)

Steps I've taken to troubleshoot:
1. DebugViewer not throwing errors
2. Checked for typos etc. (maybe incomplete)

The required functionality is:
1. User can see number of lockpicks remaining on character load
2. User can count number of safeboxes/treasure chests lockpicked

I'm quite new and learning, so any input would be appreciated, thanks in advance.

Screenshot of GUI
https://ibb.co/dLmXgs1

txt file
Code:

## Title: TreasureCounter
## Author: xgoku1
## Version: v0.1
## Description: Counts number of treasure chests opened.
## APIVersion: 100033 100034
## SavedVariables: TreasureCounterSavedData
TreasureCounter.lua
TreasureCounter.xml

xml file
Code:

<GuiXml>
        <Controls>
                <TopLevelControl name="TreasureCounterWindow" mouseEnabled="true" movable="true" clampedToScreen="true" hidden="false">
                        <Dimensions x="150" y="200"/>
                        <Anchor point="CENTER" relativeTo="guiRoot" relativePoint="CENTER" offsetX="650" offsetY="80"/>
               
                        <OnMoveStop>
                                TreasureCounter.OnIndicatorMoveStop()
                        </OnMoveStop>
               
                        <Controls>
                                <Backdrop name="$(parent)Backdrop" edgeColor="FF0000" centerColor="6495ED" alpha="0.6" >
                                        <Dimensions x="380" y="130" />
                                        <Anchor point="TOPLEFT"  relativeTo="$(parent)" relativePoint="TOPLEFT"/>
                                        <Edge edgeSize="6" />
                                </Backdrop>
                               
                                <Label name="$(parent)Heading" width="50" height="30" font="ZoFontWinH1" inheritAlpha="true" color="FF0000" wrapMode="TRUNCATE" verticalAlignment="TOP" horizontalAlignment="CENTER" text="Treasure Counter">
                                        <Anchor point="TOP" relativeTo="$(parent)Backdrop" relativePoint="TOP"/>
                                </Label>
                               
                                <Label name="$(parent)Label1" width="30" height="10" font="ZoFontWinH1" inheritAlpha="true" color="FF0000" wrapMode="TRUNCATE" verticalAlignment="TOP" horizontalAlignment="LEFT" text="Number of lockpicks: ">
                                        <Anchor point="TOP" relativeTo="$(parent)Heading" relativePoint="LEFT" offsetY="40"/>
                                </Label>
                               
                                <Label name="$(parent)Label2" width="30" height="10" font="ZoFontWinH1" inheritAlpha="true" color="FF0000" wrapMode="TRUNCATE" verticalAlignment="TOP" horizontalAlignment="LEFT" text="Number of chests opened: ">
                                        <Anchor point="TOP" relativeTo="$(parent)Label1" relativePoint="LEFT" offsetY="40"/>
                                </Label>
                        </Controls>
                </TopLevelControl>
        </Controls>
</GuiXml>

lua file
Code:

--Variables
TreasureCounter = {} --main table
TreasureCounter.name = "TreasureCounter" --addon name for checking OnAddonLoaded
TreasureCounter.locks = 0
TreasureCounter.tc = 0
--For testing it will show number of lockpicks remaining.

--function OnAddonLoaded which is called from EVENT_ADDON_LOADED
function TreasureCounter.OnAddonLoaded(event, addonName)
        if addonName == TreasureCounter.name then
                TreasureCounter.Initialize() --calls Initialize() if addonName matches
        end
end

--function Initialize() called from OnAddonLoaded
function TreasureCounter.Initialize() --function called after OnAddonLoaded
        TreasureCounter.locks = GetNumLockpicksLeft("player")
        TreasureCounter.savedVariables = ZO_SavedVars:NewCharacterId("TreasureCounterSavedData", 1, nil, {})
        TreasureCounterWindowLabel1:SetText("Number of lockpicks: " .. TreasureCounter.locks)
        TreasureCounterWindowLabel2:SetText("Number of chests opened: " .. TreasureCounter.tc)
        TreasureCounter.RestorePosition()
end

--called from Initialize() to restore
function TreasureCounter.RestorePosition()
        local left = TreasureCounter.savedVariables.left
        local top = TreasureCounter.savedVariables.top
       
        TreasureCounterWindow:ClearAnchors()
        TreasureCounterWindow:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, left, top)
end

--called from OnMoveStop in xml
function TreasureCounter.OnIndicatorMoveStop() --get new moved var
        TreasureCounter.savedVariables.left = TreasureCounterWindow:GetLeft()
        TreasureCounter.savedVariables.top = TreasureCounterWindow:GetTop()
end


function TreasureCounter.CountIncrement(event)
        TreasureCounter.tc = TreasureCounter.tc + 1
        TreasureCounter.locks = GetNumLockpicksLeft("player")
        TreasureCounterWindowLabel1:SetText("Number of lockpicks: " .. TreasureCounter.locks)
        TreasureCounterWindowLabel2:SetText("Number of chests opened: " .. TreasureCounter.tc)
        EVENT_MANAGER:UnregisterForEvent(TreasureCounter.name, EVENT_LOCKPICK_SUCCESS)
end

--Events
EVENT_MANAGER:RegisterForEvent(TreasureCounter.name, EVENT_ADDON_LOADED, TreasureCounter.OnAddonLoaded) --registering for event "addon load"
EVENT_MANAGER:RegisterForEvent(TreasureCounter.name, EVENT_LOCKPICK_SUCCESS, TreasureCounter.CountIncrement) --registering for event "lockpick success"


votan 03/16/21 07:41 AM

If you move the window, do you get a nil error ?

Did you check for case-sensitive typos in the addon name and/or trailing white space in the lua filename line?

xgoku1 03/16/21 08:48 AM

Thank you for the response.

Quote:

Originally Posted by votan (Post 43475)
If you move the window, do you get a nil error ?

Yes. This is the debugviewer message:
user:/AddOns/TreasureCounter/TreasureCounter.lua:35: attempt to index a nil value
|rstack traceback:
user:/AddOns/TreasureCounter/TreasureCounter.lua:35: in function 'TreasureCounter.OnIndicatorMoveStop'

Line 35 is: TreasureCounter.savedVariables.left = TreasureCounterWindow:GetLeft() in function OnIndicatorMoveStop

Quote:

Originally Posted by votan (Post 43475)
Did you check for case-sensitive typos in the addon name and/or trailing white space in the lua filename line?

I believe so, just checked again now. All the names are set as "TreasureCounter" and I've not used any "self" definitions so as to avoid confusion.

Shinni 03/16/21 09:22 AM

It's EVENT_ADD_ON_LOADED instead of EVENT_ADDON_LOADED.
That's why your initialization function is not executed.

xgoku1 03/16/21 09:33 AM

Quick update;

OK, so the addon started working after restarting the game. I was trying to debug using /reloadui earlier, while tinkering with the code. I didn't make any changes since posting this thread but it seems to work now: https://ibb.co/M56wyqg

However, I think there are still some problems with Initialize() or event-calling logic in my code, as the GUI doesn't populate number of lockpicks until CountIncrement() is called by the EVENT_LOCKPICK_SUCCESS register.

xgoku1 03/16/21 10:00 AM

Quote:

Originally Posted by Shinni (Post 43477)
It's EVENT_ADD_ON_LOADED instead of EVENT_ADDON_LOADED.
That's why your initialization function is not executed.

Thanks for catching that! I can't believe I missed that :confused:

I changed the event register to EVENT_ADD_ON_LOADED, but its still not going to Initialize().

I'm seeing this error in DebugViewer:

Code:

user:/AddOns/TreasureCounter/TreasureCounter.lua:18: function expected instead of nil
|rstack traceback:
user:/AddOns/TreasureCounter/TreasureCounter.lua:18: in function 'TreasureCounter.Initialize'
user:/AddOns/TreasureCounter/TreasureCounter.lua:11: in function 'TreasureCounter.OnAddonLoaded'


Shinni 03/16/21 10:15 AM

NewCharacterIdSettings instead of NewCharacterId

xgoku1 03/16/21 10:30 AM

Quote:

Originally Posted by Shinni (Post 43480)
NewCharacterIdSettings instead of NewCharacterId

:o Yeah that was it. Thanks for checking my code, everything seems to work fine now: https://ibb.co/qkzY4cZ :banana:


All times are GMT -6. The time now is 06:05 PM.

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