Sound Effects
From GMod Wiki
Lua: Sound Effects |
Description: | How do you make sounds? How do you control looped sounds? Answers here. |
Original Author: | Joudoki |
Created: | 31st May 2008 |
There isn't a single entity or weapon that wouldn't benefit from good sound effects. What would a rollermine's shock be without the BANG? I'm here to teach you ( or to provide a reference for the rest of you ) to create sound effects.
Contents |
Finding the Right Sound
The hardest part of adding a sound effect is finding the sound itself. You have to search through all the sound files, and since most of them are in the .gcf files, this isn't an easy task; I recommend using an in game browser to find these sounds. If nothing else, you can use the Hammer Editor to look for sounds; open the Source SDK from steam, and select the Half Life 2 configuration. Open hammer, and create a new map. Then, place an ambient_generic entity; open the properties by selecting it and pressing alt+enter, then click on the sound property, and click browse. From here, you should be able to find a sound.
General Sound Info
There are generally four things that you need to know to play a sound:
- The File Path: This is the path to the file, relative to the sound/ directory; so, if your file is garrysmod/sound/mymod/mysound.wav, then you would use mymod/mysound.wav.
- The Pitch: The pitch is how "high" or how "low" the sound is; if you don't know what pitch is, think of the difference between mens' and women's voices; the men's voices are generally lower in pitch, while womens' are generally higher.
- The Volume: The higher the volume, the louder it is.
- Where you want to play it; this is generally managed as an entity that emanates the sound. If you want to just make a sound in the middle of nowhere, you can use a point_info for the entity.
Single-Play Sounds
For single play sounds, such as an electric discharge, a gunshot, etc, it is generally best to use Entity:EmitSound. The function takes three arguments; the file path, the volume, and the pitch. As an example, I want to play a sound (npc/roller/mine/rmine_reprogram.wav) at 150% of regular volume and at 50% of regular pitch, and I want it to emanate from a roller mine:
-- Where rollermine is an entity you got from somewhere else rollermine:EmitSound( "npc/roller/mine/rmine_reprogram.wav", 150, 50 );
This works well for single-run sounds; however, when you need to control a sound that loops, it's best to use the CSoundPatch.
For More Info, see Entity:EmitSound.
Looping Sounds
For looping sounds, it's better to use a CSoundPatch; The benefits come from the fact that you can change the pitch and the volume while the sound is playing, as well as being able to stop it or fade it out at any time. To create the sound, you use the CreateSound function, which will return a CSoundPatch object. As an example, I will create a sound that emanates from a source, that loops the npc/roller/mine/rmine_moveslow_loop1.wav sound:
-- Where 'source' is an entity loopingSound = CreateSound( source, "npc/roller/mine/rmine_moveslow_loop1.wav" );
After you create the entity, you can make it play using the CSoundPatch:Play and CSoundPatch:PlayEx functions. The difference between the two is that you can specify a starting volume and pitch with the CSoundPatch:PlayEx function.
-- Where 'source' is an entity loopingSound = CreateSound( source, "npc/roller/mine/rmine_moveslow_loop1.wav" ); loopingSound:Play();
Whenever you choose, you can then call the CSoundPatch:ChangePitch and CSoundPatch:ChangeVolume functions at any time to change the pitch or volume. For the CSoundPatch:ChangePitch function, the only argument is the pitch ( as a percentage ), which must be between 0 and 255, or console errors will be generated. For the CSoundPatch:ChangeVolume function, the only argument is the volume, which is a float. Unlike the pitch function, this number must be between 0 ( off ) and 1 ( full volume ).
-- Where 'source' is an entity loopingSound = CreateSound( source, "npc/roller/mine/rmine_moveslow_loop1.wav" ); loopingSound:Play(); -- Sometime later loopingSound:ChangePitch( 150 ); -- Change the pitch to 1.5x the normal pitch loopingSound:ChangeVolume( 0.8 ); -- Change the volume to 80% volume
When you need to stop the sound, you can call the CSoundPatch:Stop function.
-- Where 'source' is an entity loopingSound = CreateSound( source, "npc/roller/mine/rmine_moveslow_loop1.wav" ); loopingSound:Play(); -- Later that day loopingSound:Stop(); -- Stop the sound
Alternatively, you can use the CSoundPatch:FadeOut function, which takes the number of seconds to fade out ( go from 100% volume to 0% volume ) over.
-- Where 'source' is an entity loopingSound = CreateSound( source, "npc/roller/mine/rmine_moveslow_loop1.wav" ); loopingSound:Play(); -- Fade me out, scotty loopingSound:FadeOut( 10 ); -- Fade out over 10 seconds