Quantcast
Help with first addon, Lua not initializing? - ESOUI
 
Thread Tools Display Modes
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
03/16/21, 07:41 AM   #2
votan
 
votan's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 552
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?
__________________
@votan73 (EU - megaserver)
  Reply With Quote
03/16/21, 08:48 AM   #3
xgoku1
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 5
Thumbs up

Thank you for the response.

Originally Posted by votan View Post
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

Originally Posted by votan View Post
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.
  Reply With Quote
03/16/21, 09:22 AM   #4
Shinni
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 164
It's EVENT_ADD_ON_LOADED instead of EVENT_ADDON_LOADED.
That's why your initialization function is not executed.
  Reply With Quote
03/16/21, 10:00 AM   #5
xgoku1
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 5
Originally Posted by Shinni View Post
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

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'
  Reply With Quote
03/16/21, 09:33 AM   #6
xgoku1
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 5
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.
  Reply With Quote
03/16/21, 10:15 AM   #7
Shinni
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 164
NewCharacterIdSettings instead of NewCharacterId
  Reply With Quote
03/16/21, 10:30 AM   #8
xgoku1
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 5
Thumbs up

Originally Posted by Shinni View Post
NewCharacterIdSettings instead of NewCharacterId
Yeah that was it. Thanks for checking my code, everything seems to work fine now: https://ibb.co/qkzY4cZ
  Reply With Quote

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

Thread Tools
Display Modes

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