S&box Wiki

Revision Difference


<cat>Audio.Intro</cat> <title>Sound Events</title> <deprecated></deprecated>⤶ # 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 * Randomly adjust volume or 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. ```cs class MyClassName⤶ class ExampleClass⤶ { static SoundEvent AttackSound = new ( "sounds/weapons/gunshot.vsnd" ); static SoundEvent ExampleSound = new ( "sounds/example.wav" ); } ``` 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>⤶ This will create a sound event called `ExampleClass.ExampleSound`. # Using Sound Events ⤶ As stated above, you can trigger sound events from animations. You can also play them in game, through entities. ⤶ ```cs⤶ 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⤶ You can directly use the SoundEvent with [Sound.Play](https://wiki.facepunch.com/sbox/Guide_to_Sounds). ⤶ # TODO⤶ ⤶ Add reference to the managed audio system once added for further usage