codeAPI Reference
s&box SoundDefinition: Categorized sound GameResource
SoundDefinition GameResource API Reference
SoundDefinition is a GameResource that defines a categorized sound for use on spawned entities (thrusters, hoverballs, emitters, etc.). The Category field allows the editor to filter definitions by type.
Type Signature
CSHARP
[AssetType(Name = "Sound Definition", Extension = "sndef", Category = "Sandbox")]
public class SoundDefinition : GameResource, IDefinitionResource, IResourcePreview
{
public const string Thruster = "thruster";
public const string Hoverball = "hoverball";
public const string Emitter = "emitter";
[Property] public string Title { get; set; }
[Property] public string Description { get; set; }
[Property] public string Category { get; set; }
[Property] public SoundEvent Sound { get; set; }
public SoundHandle Play(Vector3 position);
public SoundHandle Play(Vector3 position, GameObject parent);
protected override Bitmap CreateAssetTypeIcon(int width, int height);
public void OnPreview();
public void OnPreviewStop();
}Constants
- const string Thruster = "thruster" - Category constant for thruster sounds.
- const string Hoverball = "hoverball" - Category constant for hoverball sounds.
- const string Emitter = "emitter" - Category constant for emitter sounds.
Properties
- string Title { get; set; } - Display name for this sound definition.
- string Description { get; set; } - Optional description of the sound.
- string Category { get; set; } - Category tag used for filtering in the resource picker (e.g., "thruster", "hoverball").
- SoundEvent Sound { get; set; } - The underlying sound event to play.
Methods
Play(Vector3 position)
Plays the sound at the given world position. Returns default if no sound is set.Play(Vector3 position, GameObject parent)
Plays the sound at the given world position, parented to a GameObject. Returns default if no sound is set. The sound will follow the parent.OnPreview()
Implements IResourcePreview. Plays the sound for preview in the editor.OnPreviewStop()
Implements IResourcePreview. Stops the preview sound.Usage
Create an .sndef file to define a sound:
CSHARP
[AssetType(Name = "Sound Definition", Extension = "sndef", Category = "Sandbox")]
public class ThrusterSound : SoundDefinition
{
[Property]
public string Title { get; set; } = "Thruster Hum";
[Property]
public string Category { get; set; } = SoundDefinition.Thruster;
[Property]
public SoundEvent Sound { get; set; }
}Play the sound in code:
CSHARP
var soundDef = Resource.GetFile<SoundDefinition>("sounds/my_thruster.sndef");
soundDef?.Play(thruster.WorldPosition, thruster.GameObject);Notes
- File extension is .sndef
- Implements IDefinitionResource for metadata
- Implements IResourcePreview for editor preview
- Category property enables filtering in resource picker
- Play() with parent enables following the GameObject
- Used by thrusters, hoverballs, emitters, and other sound-emitting entities
Was this helpful?