Dialogue box UI appears on screen on load

Godot Version

4.0.2 Stable

Question

I have a UI that I made that a dialogue box. The box is loading into the scene without being called. You can see it at the top right corner of the map. The way the system is supposed to work is when you press E button on a NPC the dialogue box appears and the text starts appearing.

I use autoloading to get the files for dialogue. The fist code is below. That first file is the one from the NPC. The second file is an Autoload called DialogueController. I’ll appreciate any help.


@onready var interaction_area: InteractionArea = $InteractionArea 
@onready var sprite = $AnimatedSprite2D
#King NPC

const lines: Array[String] = [
	"Hi",
	"I'm the King of this country. My name is Edric",
	"We have called upon you to save the princess...",
	"And slay the Dragon Lord. The Dragon Lord lives in his keep",
	"Many years ago a great hero slayed the Dragon Lord.",
	"You will need the ball of light to slay the Dragon Lord."
]

	



# start the conversation when the kings apperas in the scene
# use Callable to send retrive function for interaction


func _ready():
	interaction_area.interact = Callable(self, "_on_interact")
	
	
func _on_interact():
	DialogueController.start_dialog(global_position, lines)
	sprite.flip_h = true if interaction_area.get_overlapping_bodies()[0].global_position.x < global_position.x else false
	await DialogueController.dialog_finished








class_name Dialogue_controller
extends Node


@onready var text_box_scene = preload("res://Singletons/DialogueBoxes/DialogueBox.tscn")

var dialog_lines: Array[String] = []
var current_line_index = 0

var text_box
var text_box_position: Vector2

var is_dialog_active = false
var can_advance_line = false

signal dialog_finished()


func start_dialog(position: Vector2, lines: Array[String]):
	if is_dialog_active:
		return 
	
	dialog_lines = lines
	text_box_position = position
	show_text_box()
	
	is_dialog_active = true


func show_text_box():
		text_box = text_box_scene.instantiate()
		text_box.finished_displaying.connect(on_text_box_finished_displaying)
		get_tree().root.add_child(text_box)
		text_box.global_position = text_box_position
		text_box.display_text(dialog_lines[current_line_index])
		can_advance_line = false
		
func on_text_box_finished_displaying():
	can_advance_line = true

	

func _unhandled_input(event):
	if (event.is_action_pressed("advance_dialog") && is_dialog_active && can_advance_line):
		text_box.queue_free()
		
		current_line_index += 1
		if current_line_index >= dialog_lines.size():
			is_dialog_active = false
			current_line_index = 0
			dialog_finished.emit()
			return 
			
		show_text_box()

Would you mind properly using format code button on all of the code instead of small pieces of it? Literally all you have to do is click the button first and then CTRL+V to paste the code… That’s like the most basic level of respect towards others since you’re asking them to spend their valuable time on your issue. Not being rude, just direct.

Many things I am happy to point out here once you format the code.

The problem line could be show_text_box() in func _unhandled_input(event). You should only have one way to show dialog, one entry point so to speak, which in your case it seems to be via “interaction_area.interact” which when triggered calls _on_interact which shows the dialogue. So maybe this is the problem.

Cheers!

2 Likes

Ok thank you for you help