S&box Wiki

Revision Difference

Guide_to_Sounds#560953

<cat>Audio.Intro</cat> <title>Guide to Sounds</title> <deprecated></deprecated>⤶ # A beginners guide to sounds This guide assumes you've already [created a sound](Importing_and_creating_assets#creatingassets) or [imported a sound](Importing_and_creating_assets#sounds). This guide assumes you've already [created a sound event](Importing_and_creating_assets#creatingassets) or [imported a sound](Importing_and_creating_assets#sounds). ## Adjustable Properties In the inspector, you can adjust values like **volume**, **pitch**, and **decibels**. If your sound is supposed to be a screen sound, you will also need to select the **UI** option. From here, you can also [upload the sound to asset.party](UploadingAssets). <upload src="aa125/8dad92f890b4dd9.png" size="26534" name="image.png" /> <upload src="3c1ff/8dc34a5072b66e1.png" size="13603" name="sbox-dev_eI515fcWxo.png" /> Saving this file via <key>CTRL</key> + <key>S</key> or clicking on Full Recompile will compile both the source sound and the sound event. <upload src="aa125/8dad92f97e60b07.png" size="11294" name="image.png" /> <upload src="aa125/8dad92dde4082e1.png" size="3896" name="image.png" /> <upload src="3c1ff/8dc34a51e241ab1.png" size="4428" name="sbox-dev_E1SFeB8z9S.png" /> <upload src="3c1ff/8dc34a52878a693.png" size="3327" name="sbox-dev_zqa30Pd6L3.png" /> ## Looping Sounds For looping sounds you will have to either: - Place loop markers inside your sound file - Left-click the source sound in the assets browser and select the loop checkbox in the inspector. This will only work with **.wav** and **.mp3** files. ⤶ <upload src="aa125/8dad92dc9a457f6.png" size="9502" name="image.png" />⤶ ⤶ ## Playing a Sound from Code⤶ ⤶ To play the sound in-game, you should use one of the static methods from the [Sound](https://asset.party/api/Sandbox.Sound) class, using the name of the **.sound** asset type without the extension:⤶ ⤶ ## Playing a Sound from UI⤶ ⤶ You can play sound events from Panel code with [Panel.PlaySound](https://asset.party/api/Sandbox.UI.Panel/PlaySound) or using css properties [sound-in and sound-out](https://wiki.facepunch.com/sbox/custom-style-properties)⤶ ⤶ ## Playing a Sound from the World⤶ ⤶ You can play sound events from GameObject or Component code with [Sound.Play](https://asset.party/api/Sandbox.Sound/Play), this will give you a [SoundHandle](https://asset.party/api/Sandbox.SoundHandle) you can modify.⤶ Additionally you can use [RPCs](https://docs.facepunch.com/s/sbox-dev/doc/rpc-messages-u5EwxSsBrD) to broadcast sounds in multiplayer.⤶ ```cs public override void Spawn() protected override void OnEnabled() { // Plays the sound from an entity's position⤶ Sound.FromEntity("atm_card.insert", this); ⤶ // Plays the sound from a position in the world⤶ Sound.FromWorld("atm_card.insert", new Vector3(0, 0, 0)); ⤶ // Plays the sound from a position on your screen, useful for UI interactions⤶ // Make sure your .sound file has "2D" enabled⤶ var sound = Sound.FromScreen("atm_card.insert");⤶ ⤶ // Change the properties of the sound being played⤶ sound.SetPitch( 2f );⤶ sound.SetVolume( 0.5f );⤶ // play sound from GameObject origin⤶ var sound = Sound.Play( "example", Transform.World.Position ); sound.Pitch = 2.0f;⤶ sound.Volume = 0.5f; } ``` ⤶ <note>Sounds can be [predicted](Prediction) - if you're playing a sound in a predicted method such as Simulate you should make sure your logic is predictable, if it isn't you should appropriately wrap it with Prediction.Off otherwise you will hear nothing.</note>