Dialog Box Not Working Properly

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dev_cons

Please help! My dialog box is not working properly. I tried to follow some tutorials on yt and worked but the dialog box seems to stack each other child node. So the dialog box keeps restarting. But when i leave the collision area i can end the dialog

heres the code for the npc i used

    extends Area2D

var can_interact = false
const DIALOG = preload("res://Scenes/DialogueBox.tscn")


func _on_NPC_1_body_entered(body: Node) -> void:
	if body.name == "Player":
		can_interact = true

func _on_NPC_1_body_exited(body: Node) -> void:
	if body.name == "Player":
		can_interact = false

func _input(event: InputEvent) -> void:
	if Input.is_action_just_pressed("ui_accept") and can_interact == true:
		var dialog = DIALOG.instance()
		get_parent().add_child(dialog)

heres the code for the dialogue box

extends ColorRect
 
export var dialogPath = ""
export(float) var textSpeed = 0.05
 
var dialog
 
var phraseNum = 0
var finished = false
 
func _ready():
	$Timer.wait_time = textSpeed
	dialog = getDialog()
	assert(dialog, "Dialog not found")
	nextPhrase()
 
func _process(_delta):
	$Indicator.visible = finished
	if Input.is_action_just_pressed("ui_accept"):
		if finished:
			nextPhrase()
		else:
			$Text.visible_characters = len($Text.text)
 
func getDialog() -> Array:
	var f = File.new()
	assert(f.file_exists(dialogPath), "File path does not exist")
	
	f.open(dialogPath, File.READ)
	var json = f.get_as_text()
	
	var output = parse_json(json)
	
	if typeof(output) == TYPE_ARRAY:
		return output
	else:
		return []
 
func nextPhrase() -> void:
	if phraseNum >= len(dialog):
		queue_free()
		return
	
	finished = false
	
	$Name.bbcode_text = dialog[phraseNum]["Name"]
	$Text.bbcode_text = dialog[phraseNum]["Text"]
	
	$Text.visible_characters = 0
	
	while $Text.visible_characters < len($Text.text):
		$Text.visible_characters += 1
		
		$Timer.start()
		yield($Timer, "timeout")
	
	finished = true
	phraseNum += 1
	return
:bust_in_silhouette: Reply From: samjmiller

Can you share a link to the tutorial you used? And try to format the code so it’s more legible to us? (Highlight it, then click on the brackets above the text input box here).

https://www.youtube.com/watch?v=EgHlpayakUA
https://www.youtube.com/watch?v=GzPvN5wsp7Y&t=270s

Here are the two tutorials i used…

sorry for the bad code display. I think i fixed it

dev_cons | 2021-09-22 01:25