 Music and sound effects in Godot. Let's go. Most games have music and sound effects. They are instrumental to making your game feel good. If you take a game without sound effects and add sound effects and then you add a little background track, you're going from pretty boring to a lot better. And Godot makes it real easy. So I've got a blank game, a main scene, no 2D. We'll just do it in 2D. Talk a little bit about positional audio. But for right now, we'll go ahead and just focus on simplistic sound effect playing and music playing. I want to start from nothing. So here's a great website SFXR.me SFXR.me. It's a website called JSFixer. How do you even say that? But it lets you generate sound effects. It's based on an old tool called SFXR from 2007. This tool ruled. It was made with a flash, I want to say. But this person made it in JavaScript and that way it's a lot more accessible. And you can click what you want to make. So like that's a jump sound, power up, explosion, pick up coin. You can keep clicking it and you get different ones. You can even modify the different settings yourself. You can even mutate it, which takes it and changes it just slightly. I like that one. All you do then is you click this permalink. You can say save link as and I'm gonna go find my game which is in game dev screencast sound demo. I'm gonna actually no sorry. You just click this wait file. Open it in downloads. So you've got your file on your computer. Take that and drag it in and you've got this music file. It's read only. You can hear that play. You can see some details about it. Now to play that sound effect in a game you create a sound audio stream player. You'll see some other ones like audio stream player 2D that you can like put something in your game world that plays a sound effect and then the closer you are the louder it is. That's super cool. But beyond what we're doing here. And we'll just call this pick up SFX. And then over here in your stream you've got all these options. I almost always just do quick load and it shows all your audio files. Click open and now you'll see you can play that and you've got it. And then you've got this audio stream player node and it has a finished signal which is cool. You can say auto play or is it playing all that stuff but let's just add a button that plays our sound effect. We'll say call play sound effect. We'll say play SFX. We'll just drag it to the middle and we'll say on button pressed. Well we need a script. Put that in main. Clear out this stuff. We don't need it. Okay here we go. Click the signal when the buttons press will play our sound effect. And all you have to do then is to pick up sound effect to play. Run our game. You hear that. There you go. Pretty nice right. Really really dig that. So that's all you have to do the sound effect. And some cases some some examples of this would be like maybe you have a spaceship that has a weapon. In your spaceship script you can or in your spaceship node like you might have a scene for your spaceship for your player spaceship. You can then just create your audio stream player. Associate the stream file and then every time it's fired you just press you just call the play function. And that's you know the basics of sound effects. It's pretty intuitive and simplistic. And music is not too different. So I've got this song I made called Circus. Got some platformer vibes. I've got an org file because it's a little smaller than a wave. Godot plays org files. I'll just drag that into my project. Now we have Circus Og. And let's see. We can double click it and play it again. That's great. And here's something different from the sound effect which is I want this to loop. So all you do is you double click it. Check the loop. Click re-import. And now when that finishes playing it will play again. So let's drag it to the end. Ooh it started again. Because you usually want your background music to loop. So that's that. We'll go ahead and we'll duplicate that button. I pressed command D. You can do control D if you're on Windows or Linux. And we're gonna call this playing music. And we'll just move this over here. And who cares if it doesn't look good. We'll do the same thing. Connect the button. It has our old signal connection. We're gonna connect it here. I just want to make sure that that's not connected double connected. It is. So when you duplicate something it duplicates the signal. So you can just right click and click disconnect. Now that isn't connected. We'll go ahead and we'll just have music. I'm actually gonna call it. Yeah. We'll just call it music. We don't have that yet. Again. Audio stream player in here. You'll go to the inspector. Quick load circus.org. You can verify it's there. Can't play it though can I. Well that's okay. And you'll see here though loop is set to on so you can just verify that. And you can right go ahead and run this. I think it should work. No it won't because I didn't name it properly. So we'll just name this music. Click play. Hmm. Got an issue. Let's see what happened. Error calling from signal signal press to error calling the signal press to callable on play music press method not found. So let's go back into play music. Make sure I did it right. I'm just gonna disconnect and we'll go ahead and connect it here. Oh there's some funky junk going on there. Okay we should be good now. So we got the music playing. Play the sound effect to kind of nice. And we can even change the volume because that's pretty common. Sometimes things come in a little hot. You can do negative five decibels that'll make it less loud. And we're running again and you can even change those while it's playing so you can get a sense of what feels right. Okay that's negative five. Just change it to negative 20. That's barely coming through. We'll set it to two. Quite a bit louder. Negative five. That sounds pretty good with my audio but I don't know what my volume is at. So might be kind of a little odd. But okay great you say. What happens if we keep pressing play music? Just keeps restarting. What if we make it pause music and play music? We can check the current property. We can check the properties of audio stream players and react accordingly. So music has... I'm gonna stop that because wow that's I think I designed it to be annoying and it sure is annoying. Oh nice we've got clapping. Okay you can then check here and let's do a couple things actually. You can say on ready var. We can say pick up SFX. I want to make it so that we have some typing because that will make life a little bit easier when we're making our game. So we're gonna say pick up SFX is of type audio stream playback and we're gonna set it on ready to pick up SFX. So now that we know it's an audio stream player. Okay see you gotta get it right. It's audio stream player and then we can change this to be pick up SFX and then for music we'll do the same thing. Audio stream player and we'll set it to music. This not only lets us more easily rename our nodes in the scene but gives us nicer type printing I think. Let's see. So now we have music yes. Did that not work before? Sorry let me just verify. No see that's that's why we do these things. I sometimes wonder why and that's why. So music audio stream players have properties and let's look at the docs. This will be a good learning opportunity. If you command click or control click on that we go to audio stream player and please audios non-conditionally. Let's check a couple things. So we've got auto play defaults to false. That's like when it's added to the scene should it play automatically and I kind of think sometimes you might want to like when you add music but you can check if it's playing. That's what we care about and there's some other options here to like the volume which you could change dynamically in your code but let's go back to here and go ahead and go to main gd and so we'll do music. We're back we're back where we were sorry sometimes go from the weasel. That works. Let's go ahead instead of restarting it on its playing we'll say if music dot playing then play it else music dot. Now there's I want to say there's a way to pause it. Let's just see if stop pauses it. So oops got these wrong. That happens a lot. Getting the logic right is very important. It's playing. We're at the very beginning of the track. Let's get to a part in the track where it's different so we can check if stop pauses or goes back to zero. Okay click it again. Okay it restarts it. So that's fine that's interesting right. I want to make it to see if we can pause it but instead for now let's just go ahead and we'll say we have our button. Let me punch out of here and we've got to play music. So we'll say play music. If it's playing we'll stop it so we'll say play music dot text is equal to play music because we're saying okay the music has been stopped we want to play it again and now we'll make it stop music. Run it. Now change the stop music. Click it. Place it again. That's cool. Let's see if we can figure out how to pause an audio string. Playing. True. False. What if we just call what if we just change that property value. So instead of saying pause music we'll say resume music and then what if we say music dot playing is false and here we'll say music dot play. Does that work? Okay we're just waiting. That doesn't work. Is it because play restarts from zero? I bet you it is. Okay play from position zero. So we could keep track of the position when we start and stop and do that but I think we can instead just say it playing is true instead of calling that function. Thanks for bearing with me through this. Gosh we've got to wait to get to the good part. Okay. Restarts it. Alright let's um I don't know how to figure that out so we're gonna go ahead and we'll code it our own way. We'll say music dot stop. We'll say music dot position. There's got to be a way to get playback position. We'll say music pause is that and then here we'll say music pause. We'll say music dot play. Music pause. Pause is short for position. So we stop it. We keep track of the variable then we play it and then whenever we stop it it will be updated. So okay that worked. Interesting stuff. Always learning. This whole section is way off in the weeds but hopefully this helps you with just like basic audio playing and what you do there. I use a program called One Bit Dragon to make my music when making games but you could just find free and public domain games. You can make music the way you want to. One Bit Dragon is pretty neat. It's pretty simplistic and works well. That's audio and g'dow. The basics. Alright thanks. See ya. Bye.