Help With Recursion

Hello Guys!

So I’m trying to make a custom UI class where recursion happens when calling the same function in a table like Roact.

Here is my module code

local module = {}

local GuiTypes = {
	Frame = "Frame",
	TextLabel = "TextLabel"
}

function createScreen(name, uiType, parent, tree)
	local Gui = Instance.new(uiType)
	Gui.Name = name

	if parent then
		Gui.Parent = parent
	end

	if tree then
		if tree.Gui then
			print("Tree: ", tree)
			tree.Gui.Parent = Gui
		end
	end

	return Gui
end

function module:MountElement(name, parent, tree)
	createScreen(name, "ScreenGui", parent, tree)
end

function checkElement(Element)
	if type(Element) == "table" then
		return Element
	end
end

function module:CreateUI(name: string, uiType: string, Element)
	local Gui = createScreen(name, uiType)
	local Property = {}

	if type(Element) == "table" then
		for i, v in pairs(Element) do
			Property[i] = v
		end
	end

	print("Property:", Property)

	if Property and type(Property) == "table" then
		for Name, element in pairs(Property) do
			if element.Gui then
				if element.Element then
					local Mounted = checkElement(element.Element)
					createScreen(Name, element.UIType, Gui, Mounted)
				else
					createScreen(Name, element.UIType, Gui)
				end
			end
		end
	end

	return { 
		Gui = Gui, 
		UIType = uiType, 
		Element = Element
	}
end

return module

Here is where the error occurs:

local player = game.Players.LocalPlayer
local UIClass = require(game.ReplicatedStorage.UiClass)

local Element = UIClass:CreateUI("Hello", "Frame", {
	Recusive1 = UIClass:CreateUI("UAF", "Frame", {
		-- recursion stops at recursive 1
		Recursive2 = UIClass:CreateUI("2", "Frame", {
			-- it doesn't work here
		})
	}),
})

UIClass:MountElement("Ulric", player.PlayerGui, Element)

any help is appreciated!

I don’t understand how this is recursive. It’s not even a function(Not the UIClass:CreateUI(),the place where you call it). You are calling another function in succession and that’s it.its intended to stop there.Can u please be a bit more clear on what you are trying to achieve

When you step through the elements of an element, you don’t have to createScreen for each one, as CreateUI already did that job. All you have to do is parent your element.Gui to the Gui of the current element. I hope that makes sense.

    if Property and type(Property) == "table" then
        for Name, element in pairs(Property) do
            if element.Gui then
                --if element.Element then
                --    local Mounted = checkElement(element.Element)
                --    createScreen(Name, element.UIType, Gui, Mounted)
                --else
                --    createScreen(Name, element.UIType, Gui)
                --end
                element.Gui.Parent = Gui  <-------
            end
        end
    end

i already solved using the same thing u had. Thats a big plus

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.