Quantcast ESOUI - View Single Post - Check if control exists
View Single Post
10/05/17, 01:18 PM   #2
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 338
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
<Label name="QuestLabel" virtual="true" font="ZoFontAnnounceMedium" text="">
    <Anchor point="BOTTOMLEFT" relativeTo="$(parent)WindowTitle" relativePoint="BOTTOMLEFT" offsetX="50" offsetY="100" />
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