Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I collapse a recursive/embedded function?

This is a very confusing question, but let's imagine this scenario.

There is two tables. However, inside the table contains itself a table of the other.

local digitsofpi = {3,1,4,1,5}
local table = {"These "," are "," digits "," of Pi",digitofpi," very", "interesting"}

What if I wanted to do a function, named "printAllContent()" with one parameter which is a table, where I run through the table's content and print what is inside and a user input is necessary every time you progress through the table, such as clicking the screen.

function printAllContent(table)
	for i,v in pairs(table) do
		print(v)
		repeat
			wait()
		until UserInput -- This line isn't proper code, it just yields until the user responds
	end
end

However, when you come across a table, you will then call the same function, "printAllContent()", however this time you put the table inside the table as the parameter of the function. Now the function looks like this.

function printAllContent(table)
	for i,v in pairs(table) do
		if isTable(v) then -- This is a made up function which returns if the parameter is a table
			printAllContent(v)
		end
		print(v)
		repeat
			wait()
		until UserInput -- This line isn't proper code, it just yields until the user responds
	end
end

This way, it will run through all of the content of the table including the embedded table.

The problem here is that let's say that the user has the option to quit in the middle of the function using another function, "quitPrinting()". This function is connected to an event where a user presses a U.I button on the screen

For example, let's say the printAllContent() function is called and you prematurely end the process by calling "quitPrinting()" when you get to '4'

These 
are 
digits 
of
Pi 
3
1
4

And you called the function,printAllContent(), again and you get this

These 
very
are
interesting
digits
of
Pi 
3
1
4
1
5
very
interesting

While the embedded function was closed, every function call that came before it continued to print. How do I, essentially, stop all function when one recursive function ends.

I hope this made sense, this is a very difficult problem to describe.

For additional context. I am making my own npc dialog system and I'm using tables as the script for the npc tor read, and I also have a button where the player can leave the conversation at any time.

tl;dr In a recursive function, how do I stop all previous functions.

1 answer

✓ Accepted answer

Seems like a bool variable should do the trick. I hope i did not complicate things too much.

local collapse = false --this variable is outside function, so it will preserve its value
local nestingLevel = 0 --we need to remember how many times the function nested itself, so we can reset collapse variable

function printAllContent(table)
	nestingLevel = nestingLevel + 1
	for i,v in pairs(table) do
		if isTable(v) then 
			printAllContent(v)
		end
		if collapse then
			nestingLevel = nestingLevel - 1
			if nestingLevel = 0 then collapse = false	end
			return --it will end current function
		end
		print(v)
		repeat
			wait()
		until UserInput 
		--"quit" is my guess only, feel free to adjust
		if UserInput == "quit" then
			collapse = true
			nestingLevel = nestingLevel - 1
			if nestingLevel == 0 then	collapse = false end
			return --it will end current function
		end
	end
	nestingLevel = nestingLevel - 1
end

Edit: If nestingLevel seems too confusing, you can also manually reset collapse variable after the dialogue ends.

local collapse = false

function printAllContent(table)
	for i,v in pairs(table) do
		if isTable(v) then 
			printAllContent(v)
		end
		if collapse then
			return --it will end current function
		end
		print(v)
		repeat
			wait()
		until UserInput 
		--"quit" is my guess only, feel free to adjust
		If UserInput == "quit" then
			collapse = true
			return --it will end current function
		end
	end
end

--somewhere in your code collapse = false

1 comment

0

Ah, that's a shame, I thought there would be a more elegant answer. Thank you nonetheless!

This discussion is preserved as it was. New answers, comments, and votes are closed.