Godot Version
4.4
Question
hi, I think I’m very close to figuring out this dang parser but I’m having trouble with it branching down. My script is as follows and SHOULD place dictionaries within the dictionary of the previous choice, but right now its basically creating a new dictionary each line.
→ choice
\ttab
\ttab 2
\ttab 3
→ choice check same line
\ttabariono
\ttaby
\t → choice 2
\t \ttab 4
\t \ttab 5
\t → choice 3
\t \ttab 6
\t \ttab 7
^ script
Output:
8: [{ 8: [“-> choice”, [1]], 9: [“\ttab”, 0], 10: [“\ttab 2”, 0], 11: [“\ttab 3”, 0] }, [1]], 12: [{ 12: [“-> choice check same line”, [1]], 13: [“\ttabariono”, 0], 14: [“\ttaby”, 0], 15: [{ 15: [{ 15: [“\t → choice 2”, [1]], 16: [“\t\ttab 4”, 0], 17: [“\t\ttab 5”, 0] }, 1], 16: [{ 16: [“\t → choice 2”, [1]], 17: [“\t\ttab 5”, 0] }, 1], 17: [{ 17: [“\t → choice 2”, [1]] }, 1], 18: [{ 18: [“\t → choice 2”, [1]], 19: [“\t\ttab 6”, 0], 20: [“\t\ttab 7”, 0] }, 3], 19: [{ 19: [“\t → choice 2”, [1]], 20: [“\t\ttab 7”, 0] }, 3] }, 1] }, [1]],
bold is where the issue starts, it creates choice 2 again but with 16: as the key instead.
thank you so much for the help. I know could probably be saving myself a headache with JSON files but I’m new to coding well, anything and really want to learn how to do this for any future thing I create.
CODE:
func startNest(ID, choiceName, passedIndent) -> Dictionary:
var nextLineNumber = ID + 1
var nextText = allScript[ID + 1]
var assignedLine = assignLineType(allScript[ID])
var oldIndent: int = passedIndent
var indent: int
var text = choiceName
var FINALRESULTS = {} #need to place the results in this
var choName = choiceName #makes the name changable through itterations
while nextText.contains("\t") == true: #while there is a possibility of the next tab having a \t
#there cannot be a choice without a \t under it wheather its a dialog, jump, or detour IT NEEDS TO BE THERE
#somthing is off with dialog creator you need to get the new indent somehow
#if indent/ new indent is greater than or equal to passed in indent
if text.contains("->") == true: #is it a choice?
var regex = RegEx.new() #gets indents if its a choice
regex.compile("\t")
var result = regex.search(text)
if result != null:
var string = result.get_string()
indent = string.length()
else:
indent = 0
if indent == oldIndent:
if indent == 0: #breaks the loop if its the first iteration of the choice - for the original create dict script
FINALRESULTS = dialogChoice(ID, choName, indent)
break
else:
FINALRESULTS[ID] = [dialogChoice(ID, choName, indent), assignedLineType]
#update everthing INCLUDING ID, TEXT AND CHONAME NOT NEXT NEXT ONES !!!!!!!!!!
oldIndent = indent
choiceName = nextText
ID = nextLineNumber
nextLineNumber += 1
nextText = allScript[nextLineNumber]
elif indent > oldIndent:
continue
else:
nextLineNumber += 1
nextText = allScript[nextLineNumber]
continue
#continues checking for choices on same or greater level, if it find one on a less it skips it
else: #if its not a choice ignore it and continue to next line
nextLineNumber += 1
nextText = allScript[nextLineNumber]
continue
return FINALRESULTS
#looping over different function
#create dialog branch > creat dialog > when it gets to another branch do the following
#if tabs > older - start a new nest
#if its = to older - return back to called function and finish loop- use returned branch as new information
#if its < than tab back again with the new info
#if its 0 then end the function and return the dictionary
func dialogChoice(ID: int, choiceName, passedIndent: int) -> Dictionary:
var nextLineNumber: int = ID + 1
var nextText: String = allScript[ID + 1]
var text: String = choiceName
choiceName = {}
choiceName[ID] = [text, [lineType.CHOICE]]
dialogCreator(nextLineNumber, nextText, passedIndent, choiceName )
return choiceName
func dialogCreator(ID: int, text: String, passedIndent: int, passedDictionary: Dictionary):
var assignedLine = assignLineType(allScript[ID])
var nextLineNumber = ID + 1
var nextText = allScript[ID + 1]
var indent: int
if text.contains("->") == true: #gets indents
var regex = RegEx.new()
regex.compile("\t")
var result = regex.search(text)
if result != null:
var string = result.get_string()
indent = string.length()
else:
indent = 0
if indent > passedIndent:
passedDictionary[ID] = [startNest(ID, text, indent), assignedLine]
return #check for text MAY HAVE TO REMOVE THE FACT THAT ITS A STRING IN FUNCTIONS
if indent <= passedIndent:
return
var packedStringSize = allScript.size()
if nextLineNumber < packedStringSize:
if text.contains("->") == false: #if its not a choice
if text.contains("\t") == true: #if it tabbed
passedDictionary[ID] = [text, assignedLine] #bread and potatoes THIS IS WHAT MAKES THE TEXT
dialogCreator(nextLineNumber, nextText, passedIndent, passedDictionary)
return
else:
return
else:
return passedDictionary
print("SOMEHOW YOU GOT A CHOICE TO BYPASS THE FIRST IF,,, HOW")