Sound In Code: Difference between revisions
Beerdude26 (talk | contribs) (Added playing sounds via VGUI interface) |
m (→AI sounds) |
||
(20 intermediate revisions by 11 users not shown) | |||
Line 1: | Line 1: | ||
{{LanguageBar}} | |||
== Emit sound from entity == | == Emit sound from entity == | ||
Use <code>EmitSound</code> to emit a sound from | Use <code>EmitSound</code> to emit a sound from an entity's origin: | ||
<source lang="cpp">EmitSound( "soundscript.file" );</source> | |||
<code>EmitSound( " | The sound will follow whichever entity it is assigned to, based on the entity index provided to <code>EmitSound</code>. If an entity index is provided, the position overload argument will not affect the position of the sound; to override this behaviour and have the sound play at the desired point in space, pass in -1 as the entity index: | ||
<source lang="cpp">EmitSound( filter, -1, "Weapon_ProxyBomb.LeadIn", &soundPosition );</source> | |||
See the <code>CSoundEmitterSystem</code> class in SoundEmitterSystem.cpp for overloads and implementation details. | See the <code>CSoundEmitterSystem</code> class in SoundEmitterSystem.cpp for overloads and implementation details. | ||
Be sure to precache the sounds you use using <code>PrecacheScriptSound</code> in your entity's <code>Precache</code> method before use: | |||
<source lang="cpp"> | |||
void MyEntity::Precache( void ) | |||
{ | |||
BaseClass::Precache(); | |||
PrecacheScriptSound( "soundscript.entry" ); | |||
} | |||
</source> | |||
== Play sounds anywhere == | == Play sounds anywhere == | ||
To play a sound on the client, use the following code: | To play a sound on the client and without spatialisation (i.e. as an "ambient" sound), use the following code: | ||
< | <source lang="cpp">enginesound->EmitAmbientSound( "link/to/soundfile", 1.0f );</source> | ||
You may need to include <code>#include "engine/ienginesound.h"</code> in order to get access to the interface. | You may need to include <code>#include "engine/ienginesound.h"</code> in order to get access to the interface. | ||
Line 15: | Line 31: | ||
== Play sounds via VGUI interface == | == Play sounds via VGUI interface == | ||
<source lang="cpp"> | <source lang="cpp"> | ||
include "vgui/ISurface.h" | #include "vgui/ISurface.h" | ||
#include "vgui_controls/Controls.h" | |||
using namespace vgui; | using namespace vgui; | ||
surface()->PlaySound( "common/talk.wav" ); // Starts from the 'sound' folder | |||
</source> | </source> | ||
== AI sounds == | |||
AI sounds are not audible to the player, but can be heard and reacted to by NPCs. | |||
<source lang="cpp"> | |||
#include "soundent.h" | |||
CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), SOUNDENT_VOLUME_PISTOL, 0.2, GetOwner() ); | |||
</source> | |||
''InsertSound(TYPE OF SOUND, Origin of noise, Volume Level, Duration, owner)'' | |||
'''Type of Sound''' is used to control how sounds are heard in the world. ''ex:'' SOUND_COMBAT allows '''[[AI]]/NPCs''' to react to sounds. | |||
* {{ent|ai_sound}} | |||
* {{ent|soundent}} | |||
== See Also == | |||
*[[Sound and Music]] | |||
*[[Single-Player Mapping Tips]] | |||
*[[Adding More Detail to a map]] | |||
[[Category:Sound System]] | [[Category:Sound System]] | ||
[[Category:Special Effects]] | |||
[[Category:Programming]] | [[Category:Programming]] | ||
[[Category:AI]] |
Latest revision as of 19:23, 18 July 2025
Emit sound from entity
Use EmitSound
to emit a sound from an entity's origin:
EmitSound( "soundscript.file" );
The sound will follow whichever entity it is assigned to, based on the entity index provided to EmitSound
. If an entity index is provided, the position overload argument will not affect the position of the sound; to override this behaviour and have the sound play at the desired point in space, pass in -1 as the entity index:
EmitSound( filter, -1, "Weapon_ProxyBomb.LeadIn", &soundPosition );
See the CSoundEmitterSystem
class in SoundEmitterSystem.cpp for overloads and implementation details.
Be sure to precache the sounds you use using PrecacheScriptSound
in your entity's Precache
method before use:
void MyEntity::Precache( void )
{
BaseClass::Precache();
PrecacheScriptSound( "soundscript.entry" );
}
Play sounds anywhere
To play a sound on the client and without spatialisation (i.e. as an "ambient" sound), use the following code:
enginesound->EmitAmbientSound( "link/to/soundfile", 1.0f );
You may need to include #include "engine/ienginesound.h"
in order to get access to the interface.
Play sounds via VGUI interface
#include "vgui/ISurface.h"
#include "vgui_controls/Controls.h"
using namespace vgui;
surface()->PlaySound( "common/talk.wav" ); // Starts from the 'sound' folder
AI sounds
AI sounds are not audible to the player, but can be heard and reacted to by NPCs.
#include "soundent.h"
CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), SOUNDENT_VOLUME_PISTOL, 0.2, GetOwner() );
InsertSound(TYPE OF SOUND, Origin of noise, Volume Level, Duration, owner)
Type of Sound is used to control how sounds are heard in the world. ex: SOUND_COMBAT allows AI/NPCs to react to sounds.