Sound In Code: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Extra clarification to EmitAmbientSound to show that sounds produced via this method are unspatialised)
m (Rewording)
Line 5: Line 5:
<source lang="cpp">EmitSound( "soundscript.file" );</source>
<source lang="cpp">EmitSound( "soundscript.file" );</source>


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 provided position overload 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:
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>
<source lang="cpp">EmitSound( filter, -1, "Weapon_ProxyBomb.LeadIn", &soundPosition );</source>



Revision as of 04:06, 23 September 2021

Template:Otherlang2

Emit sound from entity

Use EmitSound to emit a sound from a base entity:

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.

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