CSoundPatch/CreateSound cuts out after abitrary length of time.

I’m attempting to play a 20 second long 44100 Hz. wav clip using CreateSound(). The sound plays fine, but as the title says, cuts out.

Here’s the code, which is being run serverside:


local snd = Sound("ambience/test.wav")
local ply = Entity(1)
ply.ambience = CreateSound(ply,snd)
ply.ambience:SetSoundLevel(0)
ply.ambience:Play()

Anyone know what’s up?

The garbage collector cleans up the sound variable before it’s ended. It’s because the sound is local and going out of scope. Try make it global and post results.

What?

First, the “snd” variable is simply the string itself,

Sound returns the string you pass to it after it precaches the sound, which means that createsound takes a string as the second parameter, not a magical sound object.

Second, he’s saving the csoundpatch on the player itself as you can see from ply.ambience, so nothing is really garbage collected there other than a string.

I don’t know what I was thinking, thanks for clarifying.

If you start another sound with CreateSound with the exact same filepath and exact same entity, the previous one will be stopped instantly.

CreateSound( Entity(1), “mymusic.mp3”) – Is 60 seconds long
timer.Simple( 31, function()
CreateSound( Entity(1), “mymusic.mp3”) – At 31 seconds, this sound will start playing and the one above will be stopped.
end )