Component that keeps background music playing. On each update it checks a shared Bgmmanager.soundHandle and (re)starts the configured SoundEvent when the handle is null, not playing, or finished.
using Sandbox;
public sealed class GameMusic : Component
{
[Property] public SoundEvent BgmEvent { get; set; }
protected override void OnUpdate()
{
if ( Bgmmanager.soundHandle == null || !Bgmmanager.soundHandle.IsPlaying )
{
Bgmmanager.soundHandle = Sound.Play( BgmEvent );
}
// Start the sound if it isn't playing yet
if ( Bgmmanager.soundHandle.Finished )
{
Bgmmanager.soundHandle = Sound.Play( BgmEvent );
}
}
}