Hey i tried to do what you said, but when i make my dialogue system scene global(autoload), i get a lot of errors, somethings like can’t acess the nodes from the scene.
Can you lend me a hand one more time? here’s my actual code without be global(autoload);
extends Control
List of dialogues
@export var dialogue_texts: Array = [
“hello.”,
“how are you?”,
“what’s your name?”
]
Current line index
var current_line: int = 0
Animation control
var is_animating: bool = false
@export var typing_speed: float = 0.05
References to the nodes
@onready var dialogue_label = $Panel/Label # Correctly points to the Label
@onready var audio_player = $AudioStreamPlayer2D
Dynamically created timer
var typing_timer: Timer = null
Current full text being animated
var current_text: String = “”
Current character index
var char_index: int = 0
Script initialization
func _ready():
self.visible = false
show_dialogue() # Show the dialogue at the start
Updates the text on the Label based on the current index
func display_current_line():
if current_line < dialogue_texts.size():
animate_text(dialogue_texts[current_line])
play_typing_sound()
else:
hide_dialogue()
Method to handle key press
func _input(event):
if event.is_action_pressed(“ui_accept”): # Detects the pressing of the “Space” key (or Enter, depending on input settings)
if is_animating:
skip_animation() # Skips the animation
else:
current_line += 1
if current_line < dialogue_texts.size():
display_current_line()
else:
hide_dialogue() # Hides the dialogue when it ends
Shows the dialogue panel and restarts the dialogue
func show_dialogue():
self.visible = true
current_line = 0
display_current_line()
Hides the dialogue panel
func hide_dialogue():
self.visible = false
stop_typing_sound()
Animates the text by displaying one character at a time
func animate_text(full_text: String):
if typing_timer:
typing_timer.queue_free()
is_animating = true
current_text = full_text
dialogue_label.text = “”
char_index = 0
typing_timer = Timer.new()
typing_timer.wait_time = typing_speed
typing_timer.one_shot = false
add_child(typing_timer)
typing_timer.start()
typing_timer.timeout.connect(Callable(self, “add_character”))
Adds a character to the animated text
func add_character():
if char_index < current_text.length():
dialogue_label.text += current_text[char_index]
char_index += 1
else:
is_animating = false
stop_timer()
stop_typing_sound()
Skips the animation and displays the full text
func skip_animation():
if typing_timer:
stop_timer()
is_animating = false
dialogue_label.text = current_text
stop_typing_sound()
Stops and removes the timer
func stop_timer():
if typing_timer:
typing_timer.stop()
typing_timer.queue_free()
typing_timer = null
Plays the typing sound
func play_typing_sound():
var sound = load(“res://sounds/scripttextsound.wav”)
if sound:
audio_player.stream = sound
audio_player.play()
Stops the typing sound
func stop_typing_sound():
if audio_player.is_playing():
audio_player.stop()