Thread Tools Display Modes
Prev Previous Post   Next Post Next
03/16/21, 05:23 AM   #1
xgoku1
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 5
Question 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"

Last edited by xgoku1 : 03/16/21 at 05:28 AM.
  Reply With Quote
 

ESOUI » Developer Discussions » Lua/XML Help » Help with first addon, Lua not initializing?


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