It’s time to add some noise to the game.
To start I made some sound effects using jsfxr, a free sound effect generator. You can check it and play around with it here.
I made 5 sound effects I intend to add to the game, a wall hit, a paddle hit, a score effect, as well as sound effects for when you start up the game and when you lose. I also wanted to add one for victory, but couldn’t generate anything I liked.
In Godot audio has been written with games in mind according to their documentation, and apparently is broken up into two main components: Audio Buses and Audio Streams.
Audio is something I never really understood in Unity, so I’m in the dark here on trying to connect it to things I already know.
For my purpose here, though, I think I can get away with only using the master Audio Bus, without worrying too much about the rest.
Audio Streams are basically the audio source, in other words, what will play my sounds. Reading into them, it seems there are different kinds of Audio Streams: AudioStreamPlayer, AudioStreamPlayer2d, and AudioStreamPlayer3d. We don’t care about positional sound, so we just need AudioStreamPlayer.
To start, let’s hook up the start game effect.
I’ve generated this WAV file that’s been imported. Now, in the scene we add in a new AudioStreamPlayer for this effect.
In the inspector I will set the stream by selecting new AudioStream sample, then drag my wav file over.
Now for the code. I just need to tell the effect to play when the player presses start.
And that works perfectly! Now to do that with the other effects.
For the wall and paddle hit sfx this gets a bit trickier, since the GameController does not know when this happens. Instead, in the Ball game scene we can add those in.
After a quick test I decided to switch them, I like them better the other way around, but here are the effects anyway:
Scoring and game over were relatively simple as well, as that code is also centralized in the Game Controller.
Some inefficiencies in here, for sure, which I hope to tackle in a refactor at some point, but for now I’m treating this like quick and dirty prototyping.
With that I have the sound effects all working. I still want to add a few more, one for launching the ball, for instance, and one for victory, but those can wait for another day.