how to make the enemy talk?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GameNoob809

I’m making a survival horror game and ran into a problem. How to make the enemy say different phrases that were pre-recorded MP3 files. At the same time, make him say them in different order (randomly). And also when reacting to the enemy, he uttered individual phrases, and when he leaves the player, he also said some phrases. Please help me please. I couldn’t find any information on this on the Internet.

:bust_in_silhouette: Reply From: DaddyMonster

Add the audio

In your enemy scene node tree with the root node selected click the “+” and select “AudioStreamPlayer2D” (or 3D as appropriate) and drag your audio file into the “stream” on the right. Name the node.

Make one of these for each sound.

Add a script

Right click on the enemy root node and click “attach script”.

var is_speaking = false

func speak(phrase):
	if is_speaking:
		return
	if phrase == 0:
		$Phrase_1.play(0.0)
	elif phrase == 1: 
		$Phrase_2.play(0.0)
	# etc
	is_speaking = true

You’ll need to call the speak method on whatever criteria you want. You might also want to add a timer node with a signal to reset is_speaking. Hopefully that puts you on the right lines.

Good luck!