+1 vote

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.

Godot version 3.3.4
in Engine by (13 points)

1 Answer

0 votes

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:
    if phrase == 0:
    elif phrase == 1: 
    # 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!

by (1,789 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.