• How do I precache a lot of sounds so that GMod doesn't crash when playing the sounds?
    5 replies, posted
Currently, I'm trying to get a SWEP to play a bit of a song 0.1 seconds at a time. However, this crashes my game when firing, even though (I think) I precached all the sounds. Here's my Deploy function, which was in Initialize until I moved it for debugging purposes. Putting it in Initialize causes the same problems as having it in Deploy, so I don't think that's it [CODE]function SWEP:Deploy() for i=0,864 do table.insert(Sounds,"weapons/rainbowroadgun/rainbowroad" .. i .. ".wav") end for i,S in pairs(Sounds) do util.PrecacheSound(S) print("Precached " .. S) end end[/CODE] This also prints about 865 lines of code saying "Precached weapons/rainbowroadgun/rainbowroad0.wav" through rainbowroad864.wav, like intended. Here's where I'm playing the files [CODE]function SWEP:PrimaryAttack() --handling shooting and stuff OkaySoFar = true if not IsValid(self) then OkaySoFar = false else if not IsValid(self.Weapon) then OkaySoFar = false else if not IsValid(self.Owner) then OkaySoFar = false end end end if not OkaySoFar then return end /*local bone = self.Owner:GetViewModel():GetAttachment(2) local tr = self.Owner:GetEyeTrace() local StartPos = bone.Pos local EndPos = tr.HitPos local Col = HSVToColor((360*CurTime())%360,1,1)*/ if self:CanPrimaryAttack() and self.Owner:IsPlayer() then if !self.Owner:KeyDown(IN_RELOAD) then self:ShootBullet(self.Primary.Damage, self.Primary.NumShots, self.Primary.Cone) self.Weapon:TakePrimaryAmmo(self.Primary.NumShots) self.Weapon:EmitSound("weapons/rainbowroadgun/rainbowroad" .. Index .. ".wav") --Here self.Owner:SetAnimation( PLAYER_ATTACK1 ) self.Owner:MuzzleFlash() self.Weapon:SetNextPrimaryFire(CurTime()+self.Primary.Delay) ChangeIndex() --render.SetMaterial( self.LASER ) --render.DrawBeam(StartPos, EndPos, 4, 12, 12, Col) end elseif self:CanPrimaryAttack() and self.Owner:IsNPC() then self:ShootBullet(self.Primary.Damage, self.Primary.NumShots, self.Primary.Cone) self.Weapon:TakePrimaryAmmo(self.Primary.NumShots) self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) self.Weapon:EmitSound("weapons/rainbowroadgun/rainbowroad" .. Index .. ".wav") self.Owner:SetAnimation( PLAYER_ATTACK1 ) self.Owner:MuzzleFlash() self.Weapon:SetNextPrimaryFire(CurTime()+self.Primary.Delay) ChangeIndex() --render.SetMaterial( self.LASER ) --render.DrawBeam(StartPos, EndPos, 4, 12, 12, Col) end end[/CODE] Index is set to 0 at the start of the code, and Sounds is set to {} Here is ChangeIndex [CODE]function ChangeIndex() if (Index==864) then Index=0 elseif (Index<864) then Index = Index + 1 end end[/CODE] There are 865 sound files, each around roughly 0.1 seconds long, zero indexed, and named like "rainbowroad0.wav" Please tell me if and how I could fix this, or to just give up. (I'd really prefer not to give up, though.) I am going to sleep now, and will check the thread when I wake up or get back from school, so hopefully you guys will have some answers for me! Thanks in advance!
fix this by not loading 1000 audio files holy shit [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/sound/PlayFile]sound.PlayFile[/url] [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/IGModAudioChannel/SetTime]IGModAudioChannel:SetTime[/url] use these to play sound instead if you really need to play specific 0.1 second segments, and just precache the whole unfragmented song file
Hey, thanks! That's exactly what I needed, I just didn't know it existed!
-snip-
[QUOTE=Mista_Epic;50162588]-snip-[/QUOTE] PlayFile isn't shared
[QUOTE=MPan1;50163000]PlayFile isn't shared[/QUOTE] Yeah, I figured that out. I just need to know how to play clips of audio, now. [editline]19th April 2016[/editline] New Thread Here: [url]https://facepunch.com/showthread.php?t=1515397&p=50163366#post50163366[/url]
Sorry, you need to Log In to post a reply to this thread.