Jump to content

Bad sound pointer?


Recommended Posts

Hi i tried to do a fade out effect when stopping music, but I did it poorly, it works fine no errors, but sometimes it gives an error, it says bad sound pointer? especially when we're more than 3 players that have the music played.

at the volume variable and the setSoundVolume(bossmusic, volume-0.1)

function bossmusic()
if isTimer(fadeout) then stopSound(bossmusic) killTimer(fadeout) end
if isTimer(removefade) then killTimer(removefade) end
bossmusic = playSound("sounds/theme.mp3", true)
setSoundVolume(bossmusic, 0.7)
end
addEvent("playmusic", true)
addEventHandler("playmusic", root, bossmusic)

function stopmusic()
if isElement(bossmusic) then 
fadeout = setTimer(function()
volume = getSoundVolume(bossmusic) 
setSoundVolume(bossmusic, volume-0.1)
end, 1000, 0)
removefade = setTimer(function()
killTimer(fadeout)
stopSound(bossmusic)
end, 15000, 1)
end
end 
addEvent("stopmusic", true)
addEventHandler("stopmusic", root, stopmusic)

 

Edited by Bean666
Link to post
  • Moderators
Posted (edited)
15 minutes ago, Bean666 said:

Hi i tried to do a fade out effect when stopping music, but I did it poorly, it works fine no errors, but sometimes it gives an error, it says bad sound pointer? especially when we're more than 3 players that have the music played.

There is a 1 second delay between validation > if bossmusic is an element. You will have to validate again. The 1 second delay is where that 'sometimes' comes in.

if isElement(bossmusic) then -- validation

  fadeout = setTimer(function()  -- delay

    -- missing revalidation

    volume = getSoundVolume(bossmusic)

    setSoundVolume(bossmusic, volume-0.1)

  end, 1000, 0)
--[[
  ...
]]

 

 

Edited by IIYAMA
Link to post
1 minute ago, IIYAMA said:

There is a 1 second delay between validation > if bossmusic is an element. You will have to validate again. The 1 second delay is where that 'sometimes' comes in.


if isElement(bossmusic) then -- validation

  fadeout = setTimer(function()  -- delay

    -- missing revalidation

    volume = getSoundVolume(bossmusic)

    setSoundVolume(bossmusic, volume-0.1)

  end, 1000, 0)
--[[
  ...
]]

 

 

function stopmusic()
if isElement(bossmusic) then 
	fadeout = setTimer(function()
        if isElement(bossmusic) then 
		volume = getSoundVolume(bossmusic) 
		setSoundVolume(bossmusic, volume-0.1)
        end
	end, 1000, 0)
	removefade = setTimer(function()
		killTimer(fadeout)
		stopSound(bossmusic)
	end, 15000, 1)
end
end 
addEvent("stopmusic", true)
addEventHandler("stopmusic", root, stopmusic)

basically like this? i add another revalidation. i got what u meant but is this right?

Link to post
  • Moderators
31 minutes ago, Bean666 said:

but is this right?

Yes

 

Maybe here as well, but you probably already figured that one out.

31 minutes ago, Bean666 said:

stopSound(bossmusic)

 

Link to post
3 hours ago, Bean666 said:


function stopmusic()
if isElement(bossmusic) then 
	fadeout = setTimer(function()
        if isElement(bossmusic) then 
		volume = getSoundVolume(bossmusic) 
		setSoundVolume(bossmusic, volume-0.1)
        end
	end, 1000, 0)
	removefade = setTimer(function()
		killTimer(fadeout)
		stopSound(bossmusic)
	end, 15000, 1)
end
end 
addEvent("stopmusic", true)
addEventHandler("stopmusic", root, stopmusic)

basically like this? i add another revalidation. i got what u meant but is this right?

Isn't it better in the first timer function to check the sound volume and once it gets to 0  => kill the timer (fadeout) and stop the sound.
This way you won't have to create another timer which will kill the first timer.
The less timers the better.

Edited by SpecT
Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...