Playing Sounds With Functions - Intermediate

2
  • 48 favourites

Stats

5,548 visits, 8,410 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

So you've made your game and put in a few sound effects, and maybe have a decent song in there as well. That's a start! Hopefully you've scanned over Adding sound - a beginner's guide

by Velojet

, as this is where you should first read in case you're not entirely sure about the Audio infrastructure of Construct 2.

If you've used the Function Plugin before, you'll know just how useful it is for consolidating your code, as code re-use is an important part of making a great running game. We'll take a look at how the sound can be triggered from any location in your code and then injected into a function dedicated to playing sounds.

You may have used a line of code such as this into your game:

This is the most basic way to play a sound. However, you may have come close to finishing your game and then you realize that some of your sounds are unmatched in volume! Or maybe you have problems with the volume of the first sound that plays (I'm not gonna mention any names in my team...). Plus maybe you realize that sounds are overlapping and create horrible artifacts that just don't sound professional.

One example I like to think of that needs critical attention would be vocals in your game. The volumes should match every other vocal sample, and you shouldn't have two overlapping vocal tracks, because then HOW ARE YOU GOING TO UNDERSTAND WHAT'S BEING SAID? So we are going to consider any vocal sounds that we use to be monophonic (one sound channel). Another thing to consider is that we'd like to classify those sounds as a "Vocal" group, so we can apply different parameters to just the vocal sfx. This is where the TAG part comes in handy from the diagram above.

After you've added some sound effects to your Sounds directory in the right column, add the Function plugin to your project. Create a new triggered Function Event On function:

I'll call this "FnPlaySFX" for Function Play Sound Effects

Next I'm going to add an Audio action within this function "Play (by name)"

Most of what you see in this diagram above probably doesn't make any sense (yet). The folder is going to be Sounds in this case (switch this to Music if you are dealing with your Music folder). The next part is where we get into the strength of Functions. In the Audio file name box we will always inject the 0th parameter of that Function. In this case, we have unlimited options for file names, so you will not need an explicit file name and will not need change this box again! The next box will be left at not looping.

The Volume box is where we get into a big part of tailoring your sounds. Any time I want to trigger a sound I have the option of attenuating the sound effect's volume in decibels (dB). In the very first diagram above you'll see that hoo1 plays at a volume of 0 dB, which means we have no reduction in volume at this point. If I inject "-3" into the 1st parameter of that Function, I will be playing that sound effect reduced by 3 dB when I trigger it. Now this means I can bring my extremely loud sound down to a volume that matches the rest of the triggered sounds!

The second part to that is useful where it subtracts the variable "sfxAtt" from that parameter as well. We've added this local variable so we can control a Global Volume throughout the game. I've used a local variable for viewing reasons but a Global Variable will be useful in the long run. This is best used when you have a "settings" menu in your game and you'd like extra customizability (this is great for when you just can't stand the volume of your sounds being Too Loud). By subtracting that variable here, we have bound ANY sound effect in the game to the volume contained in that variable and we don't need to think about it again!

The Tag (optional) box is going to contain the "group" of sounds you'd like to use. Some examples of Tag groups could be "title_music" , "game_music" , "vocals_sfx" or "explosions_sfx" which can utilize specific requirements per group. The main things I've use this for is to either a) stop a sound from overlapping itself or b) stop a sound from overlapping another specific Tag group, or c) play a specific sample by it's Tag name as opposed to it's file name (which could be ridiculously long or memory-unfriendly!). This Tag name/group will always be injected into the 3rd parameter of that Function.

Now you should have something that looks like this:

Good! Now we are onto the next part which is about tailoring individual sounds when we trigger them. We will add a Sprite into the project called "PolyphonicSfx" and the Mouse plugin to click on a trigger pad (otherwise replace this trigger pad with the part in your code where the sound will first trigger):

Here is where I call the Function "FnPlaySFX":

The 0th parameter after "FnPlaySFX" is the file name "hoo4", the 1st parameter is a 0 dB reduction, the 2nd parameter is "poly" for polyphonic sound, in which we'd like the sound to overlap itself, and the 3rd parameter is belonging to the Tag group "vocals". Wait!! This is all good for sounds that we don't mind overlapping BUT THESE ARE VOCALS!!. So we will add a Sprite into the project called "MonophonicSfx" and use the Mouse again to click on to trigger. Everything is the same except the "poly" will be changed to "mono":

So now we can click two Sprites using either polyphony or monophony, however we still need to add some code to allow the "mono" parameter to cut the previously played vocal sample. We've added a simple two-value comparison condition to allow us to check and see if we are cutting the mono sound:

Now you'll be able to hear your sample overlap or cut itself! We're almost done, the only other thing we will add is the Function to cut a sound by it's Tag name/group.

We've added the ability to cut the Tag group "vocals" just by hovering the mouse over the "CutVocalSfx" Sprite. This is best used when you have a scene in game where you need specific Tag groups currently playing to be silenced as opposed to just silencing all sounds. It's great to use this if you have longer sounds of explosions (as a basic example) playing on too long and a dialog box pops up and requires focus, and at that point loud explosions are out of place and require "termination"!

So this is the gist of the sound engine I use in all of the games I create:

Replace the mouse trigger events with whatever you'd like. This system also works with music as well! Good luck, I've included a link to a .capx file for testing purposes on my blog!

Sound Fx With Functions

Paul White

Fireskies Studios

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!