S&box Wiki

Revision Difference

Sound_Events#529678

<cat>Dev.Intro</cat>⤶ <cat>Dev.Sound</cat>⤶ <title>Sound Events</title> # Why Sound Events You just want to play a sound and are annoyed that you have to create a sound event to do it. Here's some of the things sound events can do for you. * Define whether sound is 2D or 3D * Define how far away you should be able to hear the sound * Define the volume * Randomly choose between multiple sounds each play * Randomly adjust pitch each play * Can be fired from animations # Creating Sound Events In Code You can define a sound event as a static field in any class in your addon. The class doesn't have to be an entity, it could be an empty class made especially to define sound events. ``` class MyClassName { static SoundEvent AttackSound = new ( "sounds/weapons/gunshot.vsnd" ); } ``` This will create a sound event called `MyClassName.AttackSound`. <note>The [VDC describes how to create sound events using the sdk tools](https://developer.valvesoftware.com/wiki/Half-Life:_Alyx_Workshop_Tools/Addon_Sounds). You can ignore this. We're wrapping it like this to make it easier.</note> # Using Sound Events As stated above, you can trigger sound events from animations. You can also play them in game, through entities. ``` player.EmitSound( MyClassName.AttackSound ); // play using the SoundEvent directly player.EmitSound( "MyClassName.AttackSound" ); // play using the name ``` # TODO Add more complicated creation examples once it's coded