Sound In Code: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
No edit summary
Line 17: Line 17:
<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;


vgui::surface()->PlaySound( "common/talk.wav" ); // Starts from the 'sound' folder
surface()->PlaySound( "common/talk.wav" ); // Starts from the 'sound' folder
</source>
</source>


[[Category:Sound System]]
[[Category:Sound System]]
[[Category:Programming]]
[[Category:Programming]]

Revision as of 03:38, 10 February 2019

Template:Otherlang2

Emit sound from entity

Use EmitSound to emit a sound from a base entity:

EmitSound( "soundscript.file" );

See the CSoundEmitterSystem class in SoundEmitterSystem.cpp for overloads and implementation details.

Play sounds anywhere

To play a sound on the client, 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