Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

(SOLVED)MovingSound, ITickableSound and SoundManager: starting-stopping sounds.


Darkona
 Share

Recommended Posts

Hello fellow modders and Forge coders.

 

In trying to implement a looping, moving sound, I came across a seemingly unresolvable problem.

If you use a MovingSound to, let's say, make a constant jetpack sound on a player, that moves with it, you're better off using a moving sound, logically, than making very short, continuous calls to world.playsound();

 

Case example:

The sound should start when the player turns the jetpack on and stops when he turns it off, but it should loop as long as the jetpack is working.

This can be apparently done by implementing a custom Moving sound, with the "loop" variable set to true, and an onUpdate() method setting the position of the sound to the player. No mystery here. The problem arises when you want to make the sound stop. Let's call setDonePlaying() when the jetpack is off, to indicate the system that the sound should stop.

 

Then we go to the SoundManager class, where all the sounds are processed for playing.

In the updateAllSounds method, there is a check for isDonePlaying(), and if it is set to true, there's a call to stopSound().

Making the call to stopSound() does indeed stop it, but for some reason you are not able to start it again, not even creating a new Isound object and telling SoundHandler to play it.

 

The sound manager has two methods for stopping sounds, "stopAllSounds" and "stopSound".

 

updateAllSounds() iterates across multiple lists of sounds:

- playingSounds

- tickableSounds

- delayedSounds

and others.

 

updateAllSounds does iterate over all these lists and removes the sound appropiately from each of them, as well as executes paulscode's removeSource(), so I'm at a loss at what could be preventing the sound from being played again. For some reason a reference to the ResouceLocation where the sound file is, stays around somewhere and prevents the sound from being played again, until some time has passed.

 

Any insight on this matter would be very appreciatted.

Link to comment
Share on other sites

Apparently the fact of analyzing (again) the whole deal, made me realize the answer!

 

I'll leave the solution here to help others:

 

When the updateAllSounds method detects a sound that has donePlaying set to true, it does indeed stop the sound, and it also removes the source.

HOWEVER, if the repeat variable is set to true, it will add it to the delayedSounds list AFTER it stops it and removes the source. So it will muck up the whole playing lists, making you unable to start or stop the sound again.

 

To avoid this, if your sound is a looping one, you must set the repeat variable to false as well as the donePlaying to true, at the same time.

 

Below is my CopterPackSound class, that works corectly and can be started, stopped and loops as long as the item is in use.

 

Note that it is better in my opinion to override both the fields and all the methods you need to ensure that the ones in your class are the ones actually being called. I know its not THAT necessary in some cases due to inheritance, but I find this approach a "safer" route.

 

public class CopterPackSound extends MovingSound
{

    public EntityPlayer thePlayer;
    protected boolean repeat = true;
    protected int repeatDelay = 0;

    protected float pitch;

    public CopterPackSound()
    {
        super(new ResourceLocation(ModInfo.MOD_ID, "helicopter"));
        this.repeat = true;
        this.volume = 0.8f;
        this.pitch = 1.0F;
    }

    public CopterPackSound(EntityPlayer player)
    {
        super(new ResourceLocation(ModInfo.MOD_ID, "helicopter"));
        volume = 0.8f;
        pitch = 1.0F;
        thePlayer = player;
        LogHelper.info("Sound Created");
    }

    public void setDonePlaying()
    {
        this.repeat = false;
        this.donePlaying = true;
        this.repeatDelay = 0;
    }

    @Override
    public boolean isDonePlaying()
    {
        return this.donePlaying;
    }

    @Override
    public void update()
    {
        ItemStack copter = Wearing.getWearingCopter(thePlayer);
        byte status = 0;
       if(thePlayer == null || thePlayer.worldObj == null)
        {
            setDonePlaying();
        }
        if (copter != null&& copter.hasTagCompound() && copter.getTagCompound().hasKey("status"))
        {
            status = copter.getTagCompound().getByte("status");
            if (status == ItemCopterPack.OFF_MODE)
            {
                setDonePlaying();
            }else{
                if(status == ItemCopterPack.HOVER_MODE)
                {
                    pitch = (thePlayer.motionY == 0) ? 1.0f : (thePlayer.motionY > 0) ? 1.2f : 0.8f;
                }
                else
                {
                    pitch = (thePlayer.onGround || thePlayer.motionY == 0) ?  0.8f : (thePlayer.isSneaking()) ? 0.8f : (thePlayer.motionY > 0) ? 1.2f : 1.0F;
                }
            }
        }else{
            setDonePlaying();
        }
        xPosF = (float)thePlayer.posX;
        yPosF = (float)thePlayer.posY;
        zPosF = (float)thePlayer.posZ;
    }

    @Override
    public boolean canRepeat()
    {
        return this.repeat;
    }

    @Override
    public float getVolume()
    {
        return this.volume;
    }

    @Override
    public float getPitch()
    {
        return this.pitch;
    }

    @Override
    public int getRepeatDelay(){ return this.repeatDelay; }

    @Override
    public AttenuationType getAttenuationType()
    {
        return AttenuationType.LINEAR;
    }

Link to comment
Share on other sites

  • 1 year later...

Don't hijack old threads. Make your own.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.