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()