@@ -44,6 +44,7 @@ var _current_choices : Dictionary
4444@onready var speaker_text = $ MarginContainer/PanelContainer/MarginContainer/VBoxContainer/SpeakerText
4545@onready var response_tpl = $ MarginContainer/PanelContainer/MarginContainer/VBoxContainer/ResponseTemplate
4646@onready var next_button = $ MarginContainer/PanelContainer/MarginContainer/VBoxContainer/NextButton
47+ @onready var stream_player = $ AudioStreamPlayer
4748
4849func _ready () -> void :
4950 response_tpl .visible = false
@@ -63,7 +64,9 @@ func _ready() -> void:
6364 var character_name = dialogue_data ["character_name" ]
6465 var message = dialogue_data ["message" ]
6566 var options = dialogue_data ["options" ]
67+ var audio = dialogue_data ["audio" ]
6668 show_message (character_name , message , options )
69+ play_audio (audio )
6770
6871
6972func _unhandled_input (event : InputEvent ) -> void :
@@ -78,8 +81,8 @@ func _unhandled_input(event: InputEvent) -> void:
7881
7982 # ShowMessage is only allowed to process input when visible
8083 _disable_player_movement ()
81-
82-
84+
85+
8386func show_message (speaker_name : String , message : String , choices : Dictionary ) -> void :
8487 speaker .text = speaker_name
8588 speaker_text .text = message
@@ -92,6 +95,17 @@ func show_message(speaker_name: String, message: String, choices: Dictionary) ->
9295 _current_tween .finished .connect (Callable (_on_tween_finished ).bind (choices ))
9396 show ()
9497
98+
99+ func play_audio (path : String ) -> void :
100+ if not ResourceLoader .exists (path ):
101+ return
102+ var audio = load (path )
103+ if audio is not AudioStream :
104+ return
105+ stream_player .stream = audio
106+ stream_player .play ()
107+
108+
95109## Callback when the tween typing has finished.
96110## This allows presenting the user choice options or Continue button.
97111func _on_tween_finished (choices : Dictionary ) -> void :
@@ -108,13 +122,13 @@ func _on_tween_finished(choices: Dictionary) -> void:
108122 button .pressed .connect (button_handler )
109123 else :
110124 next_button .show ()
111-
112-
125+
126+
113127## Handles canceling the typing effect based on key codes
114128func _should_end_typing (keycode :int ) -> bool :
115129 return keycode == KEY_ESCAPE or keycode == KEY_SPACE
116130
117-
131+
118132## Prevent user movement when the dialogue UI is shown
119133func _disable_player_movement () -> void :
120134 var actions = InputMap .get_actions ()
0 commit comments