Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | hudadev |
Hello!
I am currently trying to make a dialogue system for my game. I wanted to implement a feature so it would play random audios after each word in the dialogue. So, I turned the text into an array, so each word is seperated. Any idea on how to play an audio for each word in the array? (Side note: I have all the different audios saved)
var test= $Control/text.text.split(" ")
here you can see I have split up my text into an array using String.split()…
Would I use a for loop?
Sorry if this is a dumb question, I am fairly new to programming!
btw, this is my entire script:
extends Node2D
signal finished_talking
export var dialogPath = ""
export(float) var textSpeed = 0.05
var dialog
var phraseNum = 0
var finished = false
onready var text = get_node("Control/text").text
func _ready():
$Control/Timer.wait_time = textSpeed
dialog = getDialog()
assert(dialog, "Dialog not found")
nextPhrase()
func _process(_delta):
if $".".modulate != Color("00ffffff"):
var words = text.split(" ")
for word in words:
$Gus.playing = true
$Control/Indicator.visible = finished
if Input.is_action_just_pressed("ui_accept"):
if finished:
nextPhrase()
else:
$Control/text.visible_characters = len($Control/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):
$AnimationPlayer.play("fade_out")
queue_free()
emit_signal("finished_talking")
return
finished = false
$Control/text.bbcode_text = dialog[phraseNum]["Text"]
$Control/text.visible_characters = 0
while $Control/text.visible_characters < len($Control/text.text):
$Control/text.visible_characters += 1
$Control/Timer.start()
yield($Control/Timer, "timeout")
finished = true
phraseNum += 1
return
func _on_Main_zoom_gus():
$AnimationPlayer.play("fade_in")
hudadev | 2021-06-22 18:38
If you would use a for loop. You would probably end up hearing just the sound for the last word. Instead you need to play one sound at a time and wait for it to finish and then play the next sound.
StopNot | 2021-06-23 05:58