Quantcast Check if control exists - ESOUI
Thread Tools Display Modes
10/05/17, 12:51 PM   #1
ObjectMetadata
 
ObjectMetadata's Avatar
Join Date: Oct 2017
Posts: 7
Check if control exists

Hi guys,
I'm trying to check whether a (virtual) control exists before I create it. This is the XML Code
Code:
<Label name="QuestLabel" virtual="true" font="ZoFontAnnounceMedium" text="">
	<Anchor point="BOTTOMLEFT" relativeTo="$(parent)WindowTitle" relativePoint="BOTTOMLEFT" offsetX="50" offsetY="100" />
</Label>
And this is the lua code to create the control. This function gets called multiple times so I included code to check if the control already exists but it's not working

Lua Code:
  1. function TestAddon:CreateQuestLabel(questID, labelName, containingControl, yOffset)
  2.     local label = containingControl:GetNamedChild(labelName) --containingControl would be $(parent)
  3.     if label == nil then
  4.         label = CreateControlFromVirtual(labelName, containingControl, "QuestLabel")
  5.         --Fails here "Failed to create Control "QuestLabel1". Duplicate Name
  6.     end
  7.     label:SetText(questID .. " - " .. TestAddon:GetQuestName(questID))
  8.     label:SetAnchor(BOTTOMLEFT, TestAddonGridWindowTitle, BOTTOMLEFT, 50, yOffset)
  9.     return label
  10. end

Does anyone know why this does not work? Thanks!
  Reply With Quote
10/05/17, 01:18 PM   #2
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 316
Originally Posted by ObjectMetadata View Post
Hi guys,
I'm trying to check whether a (virtual) control exists before I create it. This is the XML Code
Code:
<Label name="QuestLabel" virtual="true" font="ZoFontAnnounceMedium" text="">
    <Anchor point="BOTTOMLEFT" relativeTo="$(parent)WindowTitle" relativePoint="BOTTOMLEFT" offsetX="50" offsetY="100" />
</Label>
And this is the lua code to create the control. This function gets called multiple times so I included code to check if the control already exists but it's not working
So, what I think is happening is that you're calling TestAddon:CreateQuestLabel two or more times, as follows:
TestAddon:CreateQuestLabel(questId, 'QuestLabel1', someControl, yOffset)
TestAddon:CreateQuestLabel(questId, 'QuestLabel1', anotherControl, yOffset)


So what happens is that the first time, the function creates a child of someControl, and the child has the name QuestLabel1. This is a global name. So now, this label exists. Next, you call the createQuestLabel function again. But for some reason, QuestLabel1 is passed as a labelName again. QuestLabel1 however, is a child of someControl, and not anotherControl. So of course, when you try to get the named child of anotherControl that's named QuestLabel1, it won't exist, because it's not a child of anotherControl. It's a child of someControl. So now label is nil, and it passes the first if statement. But when you try to create a virtual control with the name of QuestLabel1, it already exists, and that's where you get your error. Here's what I'd suggest. Add labelName = containingControl:GetName()..labelName and use that as the label name instead.

Last edited by Dolgubon : 10/05/17 at 01:21 PM.
  Reply With Quote
10/05/17, 01:33 PM   #3
ObjectMetadata
 
ObjectMetadata's Avatar
Join Date: Oct 2017
Posts: 7
Hi Dolgubon, thanks for your reply!
I checked what you said but the parameter "containingControl" is passed as a hard reference, so it should definitely be the same both times.

This is the function I use to call the "CreateQuestLabel" function:

Lua Code:
  1. function TestAddon:AddQuestsToGrid()
  2.     local noOfQuests = GetNumJournalQuests()
  3.     local yOffset = 50
  4.     for i = 1, noOfQuests do
  5.         local labelName = "QuestLabel" .. i
  6.         local checkboxName = "QuestCheckBox" .. i
  7.         local label = self:CreateQuestLabel(i, labelName, TestAddonGrid, yOffset)
  8.             --TestAddonGrid is the TopLevelControl in the .xml file
  9.         local checkbox = self:CreateQuestCheckBox(i, checkboxName, TestAddonGrid, label)
  10.         yOffset = yOffset + 50
  11.     end
  12. end

XML-Code:
Code:
<GuiXml>
	<Controls>
		<TopLevelControl name="TestAddonGrid" mouseEnabled="true" movable="false" clampedToScreen="true" hidden="true">
			<Controls>
				<some controls />
			</Controls>
		</TopLevelControl>
	</Controls>
</GuiXml>
The AddQuestsToGrid function gets called by a slash command.
type slash command for the first time -> everything workes as intended since the controls don't yet exist
type slash command for the 2nd time -> Duplicate error

Could this be caused by some weird object instance behaviour? Like that the object "TestAddonGrid" is not the same instance the second time as the first time?
  Reply With Quote
10/05/17, 02:10 PM   #4
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,151
I believe your problem is because GetNamedChild only returns controls that are prefixed with the parent name. When creating a control you can write "$(parent)<myname>" and it will automatically prefix it with the parent control's name. Then it should work.

But for this kind of problem you should use an ZO_ObjectPool anyways. It takes care of creating and managing your virtual controls.
__________________
>siri.exe MyAddon
Does your addon work? [y/n] n
There is a typo in there.
  Reply With Quote
10/05/17, 02:41 PM   #5
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 316
ZO_ObjectPool? What is that?
  Reply With Quote
10/05/17, 02:50 PM   #6
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,151
I'm shocked you do not know that. :O
Read this: http://wiki.esoui.com/SimpleNotebookTutorial/part7
__________________
>siri.exe MyAddon
Does your addon work? [y/n] n
There is a typo in there.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Check if control exists

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