Solved: LUA Getting Children/Regions of Frames and changing their Textures?

I’m in the process of creating a mod which has a number of boxes (frames and buttons), I’m trying to make a function that sets the colour of the previous box to black when it’s finished being used but I’m having lots of trouble doing this dynamically. Here’s what I’m using…

function ClearLastAction()
-- returns the last active frame to black so only the newest action is green
local kids = { Mod_Status:GetChildren() };
for _, child in ipairs(kids) do
    local frameName = child:GetName();
    if (frameName == lastAction) then
        print("matched frameName to lastAction")
        child:SetColorTexture(0,0,0,1);
        return
    end

All of my frames are children of Mod_Status and it finds the right frame but the Background Layer of the lastAction frame is what I need to change. I’ve tried child:SetColorTexture, I’ve tried using child:GetRegion() (and got lost in the process…) and I’ve tried child.NameOfSubFeature:SetTexture but I haven’t been able to make any headway with any of these options.

Can anyone think of a way to change the background colour in a similar way? For reference here is how the layer is defined in XML.

<Layers> 
<Layer level="BACKGROUND"> 
    <Texture name="$parent_Background" setAllPoints="true"> 
        <Color r="0" g="0" b="0" a="1" /> 
    </Texture> 
</Layer> 
</Layers>

Edit: I was able to use child:GetRegions():SetColorTexture(0,0,0,1); and that worked flawlessly. Luckily I only have one region in there so it works fine.

For future reference, the $parent in the name of the texture will be replaced with the name of the parent frame so:

<Frame name="MyFrame" >
	<Layers> 
	<Layer level="BACKGROUND"> 
	    <Texture name="$parent_Background" setAllPoints="true"> 
	        <Color r="0" g="0" b="0" a="1" /> 
	    </Texture> 
	</Layer> 
	</Layers>
</Frame>

In code, the texture can be accessed using:

MyFrame_Background:SetColorTexture(0,0,0,1)

Unfortunately that wouldn’t have worked. Though thank you for the suggestion!

The problem I experienced when trying it that way is I couldn’t concatenate a variable with a string or two variable names to make the name of a sub-element. I also couldn’t find any functions that let me search for a specific Frame by Name. MyFrame was one of about 20 that we’re being searched through to find the one that matched. I then needed to append _Background on to that but as far as I can tell there is no way to say
MyFrame .. _Background.

If I hard code it, its fine - it’s trying to make it dynamic that is killing me.

_G["MyFrame" .. "_Background"]:SetColorTexture(0,0,0,1)
or
_G[x:GetName() .. "_Background"]:SetColorTexture(0,0,0,1)
where x is MyFrame

The names of any frames/regions with one are placed in the global table.

1 Like