I sit here, on my lunch break, playing Oxygen Not Included on my laptop. I have work laptop up, ready for anything. And here I am on my desktop computer, Twitch playing my usual livestream. Sounds great, right? What's missing . . . Working on my game! Problem is, I have no idea what to do, and again I'm not feeling fully motivated. But, I am feeling motivated enough to open Unity and force myself to look at the project to find something to work on.
Since I was working on my audio last time, maybe I should continue working on that. I feel like having as many audio sources as I want for one-shot sounds is great, but what about continuous sounds, like thrusters or engines, or whatever. It wouldn't sound good to continually fire the engine sound over and over, so I need to figure out how to handle that. I could just do what I did with the one-shots, and just modify that.
So, I start there, take my sound effects source and duplicate it, rename it to something appropriate (like. . . ContinuousSoundEffectsSource! lol). This one is going to be set to loop. Now, how do I deal with this. I think maybe each object should only be allowed a single continuous sound. Probably best to use a unique identifier for the object, instead of something easy like the name. I'm going to be duplicating all kinds of stuff so there'll be a lot of similarly named objects.
So, I've identified that I need to input the Object ID, obviously I need to know the sound effect to play. I need to also know if we're starting to play something or if we're stopping what's currently playing. Maybe I need two different things, one to start and one to stop. I was originally thinking of building both functions in the same way, but now I think it should just be a fire and forget.
So, I have two custom triggers, one to start and one to stop. They both require the Object ID; to play I need the audio clip. That's actually very simple.
While I was figuring this all out, I decided to take a look at the work I did previously surrounding sounds and man, it was messy! So I cleaned it up a bit. I re-arranged the graph, added some comments, you know, made it easier to understand. Now that I'm done with that, I can add more! Two custom events, PlayContinuous with two arguments and StopContinuous with one. I guess the play function should be similar to the one shot's play function, right? Man, this is actually going to get a bit complicated.
I add a variable to the source, so I can track which object it is playing for. Wow, so complicated. I'm actually working on it as I type this out and it's breaking my brain a little. I copied a bit from the original source stuff, modifying it for being a continuous source. I had to add in a whole variable and startup check, etc. So much more to add now.
First part is simple, I loop through all detected continuous sources, if I find one that matches the ID of the object, I just use that and break the loop. But, what do I do next. . . ? No, this is too complicated. It doesn't need to be this complicated!
Starting over, let's simplify. Instead of going through a bunch of junk, let's instead just rename the objects so they match, making things so much easier. So then, we search for the object id, if we find one, yay, let's use it! All we do is set the clip and play (if it's not already playing).
Okay, that's so much easier, but is it better? Hell, does it even work? I should test it. Actually, I just realized that I need a bit more error checking. Let's add that first. Actually, you know what? Let's not bother yet. Let's give it a test to see if it works well enough yet. No, that's not a good idea. I don't want to constantly send a play command and set the clip if it's already playing that same clip. Okay, that wasn't so bad.
Now to test! Already finding stupid bugs I wasn't expecting. And a crash! Oh crap, how much progress did I lose? Oh, wow, literally all the work. Dammit! Well, not all of it, but mostly all. I don't understand why some changes stayed while others just disappear! ARGH!!!
Good thing I have my notes, I can quickly redo it all, taking into account all the mistakes I made from the beginning. So, re-reading this post I need to start by creating the functions, and making them nicer! Play seems better now, what about stop. . . . I should probably else and re-open Unity between major changes so I don't lose them all again.
Alright, I think I have the Play portion done completely, and well. Better than before! Now to make it stop properly. . . That was easy. Why did it take me so long the first time? Oh yeah, trial and error and being new, not recreating something I already did. Now to build my little test. . . Oops, looks like I totally missed building the most important part of the Player, the one that instantiates a new instance when one isn't found. . .
Now with all that fixed and testing built, it's great! And, of course, as I'm closing Unity, it decides to open a window about shutting down. Sits there for many minutes. Why does this always happen to me? Let's see what I lost during that . . . Nice, I don't think I lost anything. Sounds still working properly! YAY!!!
Edit: Sorry, I meant to post this yesterday but forgot to check the little "Published" checkbox.
Did you like this post? Tell us
Leave a comment
Log in with your itch.io account to leave a comment.