Creating a recursive nested dictionary and its not flowing correctly

Godot Version

4.4

Question

Hello, im trying to create a recursive nested dictionary for a special markdown script i’m working on (its for a dialog box in a game). I cant seem to get it to return as actually nested past the first dictionary.

#reads the textfile and adds line to a dictonary
func branchCreator(lineNumber: int, choiceEnum, choiceName): 
	#indent
	var indent: int = 0
	#text
	var text: String = choiceName
	
	var nextCheck = allScript[lineNumber + 1]
	var nextLine = lineNumber + 1
	choiceName = {}
	
	choiceName[lineNumber] = [text, choiceEnum]
	
	
	
	
	if text.contains("\t") == true: #gets amount of tabs
		var regex = RegEx.new()
		regex.compile("\t")
		var result = regex.search(text)
		print(str(result))
	elif nextCheck.contains("\t") == true:
		tabChecker(nextLine, nextCheck, choiceName )
	if indent > 0:
		tabChecker(nextLine, nextCheck, choiceName )
	
	
	
	else:
		print(choiceName.keys())
		return choiceName

func tabChecker(lineNumber: int, text, choiceName):
		var newLineNumber = lineNumber + 1
		var assignedLine = assignLineType(allScript[lineNumber])
		
		if  assignedLine == lineType.CHOICE:
			choiceName[lineNumber] = [branchCreator(lineNumber, assignedLineType,allScript[lineNumber] ), assignedLine]
		elif text.contains("\t") == true:
			tabChecker(newLineNumber, allScript[newLineNumber], choiceName)
			choiceName[lineNumber] = [text, assignedLine]

its printing :
8: [{ 8: [“-> choice”, [1]], 12: [{ 12: [“-> choice check same line”, 1], 15: [{ 15: [“\t → choice 2”, 1] }, 1], 14: [“\ttaby”, 0], 13: [“\ttabariono”, 0] }, 1], 11: [“\ttab 3”, 0], 10: [“\ttab 2”, 0], 9: [“\ttab”, 0] }, [1]], 12: [{ 12: [“-> choice check same line”, [1]], 15: [{ 15: [“\t → choice 2”, 1] }, 1], 14: [“\ttaby”, 0], 13: [“\ttabariono”, 0] }

heres the input text:
→ choice
\t tab
\t tab 2
\t tab 3
→ choice check same line
\t tabariono
\t taby
\t → choice 2
\t \t tab 4
\t \t tab 5

(the numbers/keys are just line keys for later and the choiceEnum is also not relevant as its to check what the text is in a separate area)

“choice” and “choice check same line” should be separate dictionaries and “choice 2” should be a dictionary within “choice check same line”

its also missing the double tabbed choices from “choice 2”

let me know if you need anymore information and thank you to anyone who is willing to help! If anyone had any other tips for making recursive functions I would also be super happy to hear them as I’m still very new to godot and coding in general. Thank you!

I’m having trouble understanding what output you are expecting from that input. Can you clarify what output you are expecting, and confirm that this is your exact input text:

Disregard above, I understand better from this:


Both of these functions call each other, but which one is called first as the entry point into the recursion?

  1. What is the point of this first condition text.contains("\t") == true statement block? It’s only doing a RegEx search and then printing the result.
  2. I think the second condition elif nextCheck.contains("\t") == true is not what you may be intending. Consider the input for line \t → choice 2; This ends up not running the nextCheck condition block because the first text.contains("\t") condition was true, even though the nextCheck line would be returning true.
  3. What is the point of if indent > 0: conditional? You set var indent: int = 0 at the top of the function but never modify it. This conditional block never runs.

The issues with #2 and #3 seem to explain why you may not be getting down into the next Dictionary tab level.

entry point is branchCreator

hi! thank you so much the if and elif were absolutely what was causing the dictionary to not go down.