Slowly turn down music volume

Started by m0rph84, Tue 24/01/2017 00:03:26

Previous topic - Next topic

m0rph84

Hello,
my first post here (hope not in the wrong section).
I'm stuck with what I thought was a simple problem.

What I want to achieve is to constantly turn down the volume of the background music until a certain volume level without
blocking character movement/interaction by the player.

This is what I tried to do:

Declared 3 Global variable
   

       
  • Audiochannel* chanMusic
  • Bool lowMusic = false
  • Int musicVolume =0

In my Global script I added the following function that is called in my room
when I need to turn down the volume:

Code: ags

function LowMusic()
{
   lowMusic = true;
}


and I put inside repeatedly_execute() these lines:

Code: ags

if (lowMusic == true)
  {
    while (chanMusic.Volume > musicVolume)
    {
      chanMusic.Volume --;
    }
    lowMusic = false;
  }


In my room inside room_AfterFadeIn() funtion than I call the function when needed and I assign the desired final volume to
musicVolume:

Code: ags


musicVolume = 10;
LowMusic();



and of course imported global function at the top of room file:

Code: ags

import function LowMusic();


This works but turn down the volume too quickly (because repeatedly_execute() is called many time per sec) and
if I put a Wait() I got what I want but the player interaction is blocked until the desired volume is reached.

I hope that what I wrote is clear.

Anyone have any idea on how to implement that?
I would try to avoid plugins for 2 reason, I want to learn :smiley: and I want the game to compile on Linux too.

Thanks in advance for your help and sorry for my english(non-native).

Gilbert

Try to change the 'while' in repeatedly execute to 'if' and see if it's slow enough.
If you put a while loop there, the volume will be reduced to the minimum value in one single frame.

Gurok

Quote from: Gilbert on Tue 24/01/2017 02:50:27
Try to change the 'while' in repeatedly execute to 'if' and see if it's slow enough.
If you put a while loop there, the volume will be reduced to the minimum value in one single frame.

I just wanted to elaborate on this a little. Setting lowMusic to false should be done in the else part of the if() statement. The complete section would look like this:

Code: ags
if (lowMusic == true)
{
	if (chanMusic.Volume > musicVolume)
	{
		chanMusic.Volume --;
	}
	else
	{
		lowMusic = false;
	}
}
[img]http://7d4iqnx.gif;rWRLUuw.gi

m0rph84

Quote
Try to change the 'while' in repeatedly execute to 'if' and see if it's slow enough.
If you put a while loop there, the volume will be reduced to the minimum value in one single frame.

I just wanted to elaborate on this a little. Setting lowMusic to false should be done in the else part of the if() statement. The complete section would look like this:

Code: Adventure Game Studio
if (lowMusic == true)
{
        if (chanMusic.Volume > musicVolume)
        {
                chanMusic.Volume --;
        }
        else
        {
                lowMusic = false;
        }
}

Thank you very much, It worked!
I'm also opened to alternative more efficient solution if there are any.
Ultimately I want implement a volume change function for both turn down and up cases.
Certainly I can now use a similar function for the opposite case(turn up).

Thanks again.



Matti

If I'm not mistaken, you don't need the bool lowMusic nor the function LowMusic().

In your case, as you just want the chanMusic.Volume to increase or decrease until it reaches musicVolume, all you need is

Code: ags

if      (chanMusic.Volume > musicVolume) chanMusic.Volume--;
else if (chanMusic.Volume < musicVolume) chanMusic.Volume++;


in repeatedly execute.

m0rph84

Quote
I'm not mistaken, you don't need the bool lowMusic nor the function LowMusic().

if      (chanMusic.Volume > musicVolume) chanMusic.Volume--;
else if (chanMusic.Volume < musicVolume) chanMusic.Volume++;

You are probably right! Both chanMusic and musicVolume are global variable.
I'll try and let you know.

Thanks!

m0rph84

It worked but I needed to add one control:

Code: ags

if (chanMusic != null)
  {
    if      (chanMusic.Volume > musicVolume) chanMusic.Volume--;
    else if (chanMusic.Volume < musicVolume) chanMusic.Volume++;
  }


to check if chanMusic is not empty otherwise I got a null pointer error.

Thanks again to all!

SMF spam blocked by CleanTalk