Music Volume [SOLVED]

Started by Baron, Tue 01/10/2013 16:21:49

Previous topic - Next topic

Baron

Alright, I'm stumped.  I can't get my music volume to change midstream.  I did some manual research and some forum research and the best I could come up with is:

First I created an AudioChannel pointer in my global script, thusly:
Code: ags
AudioChannel* music_channel;


Then I exported it:
Code: ags
export music_channel;


Then in my global script header I imported it:
Code: ags
import AudioChannel* music_channel;


Then I call this in Room 12:
Code: ags
aKentyTheme.Play (eAudioPriorityHigh, eRepeat);
music_channel =aKentyTheme.Play(); 


And then later, mid-Room 13 I need the volume to change, so I write:
Code: ags

music_channel.Volume = 10;


...and it says I referenced a null pointer, crashing the game.  So I try this in Room 13:
Code: ags
music_channel = aKentyTheme.Play ();
music_channel.Volume = 10;


...and it STILL says I referenced a null pointer.  So....  it becomes obvious to me that I don't have the faintest clue how audio pointers are supposed to work and why, in this instance, it seems not to.  Any, er, pointers on where I went wrong?

Thanks.

Crimson Wizard

#1
Well, first of all, whenever you are using audio channel pointer, it'd be wiser to make a check beforehand, because you can't guarantee it will point to something all the time:

Code: ags

if (music_channel)
{
   music_channel.Volume = 10;
}


Now, the question is, why is it null just after calling aKentyTheme.Play()? Hmmm... does the music actually play?


EDIT: Now, wait a second.
This does not make a sense:
Code: ags

aKentyTheme.Play (eAudioPriorityHigh, eRepeat);
music_channel =aKentyTheme.Play(); 

First time you make it play, and it returns an AudioChannel pointer, which you ignore.
Second time you make it play AGAIN.
Instead, do this:
Code: ags

music_channel = aKentyTheme.Play (eAudioPriorityHigh, eRepeat);


Ok, I got it. The error you are having is because FIRST time you started playing a sound with High priority, and then you tried to re-start them using Normal priority. Since they did not override the first played instance of the clip, they return null.

DoorKnobHandle

#2
Quote from: Crimson Wizard on Tue 01/10/2013 17:11:08
Code: ags

if (music_channel)
{
   music_channel.Volume = 10;
}


Just a minor correction, the above code would compile in C++ but doesn't compile in AGS Script, you do need to use:

Code: ags

if (music_channel != null)
{
   music_channel.Volume = 10;
}

Crimson Wizard

Quote from: DoorKnobHandle on Tue 01/10/2013 17:22:59
Just a minor correction, the above code would compile in C++ but doesn't compile in AGS Script, you do need to use:

No, it does compile :).
When pointer is the only argument in the expression, it does compare it with 0.
What does not work, is when you have a pointer used with other arguments:
Code: ags

if (music_channel || true)
{
}

In such case you would need to write:
Code: ags

if (music_channel != null || true)
{
}


DoorKnobHandle

#4
Ah, you got me there, you are right. I didn't know about that little quirk! Sorry.

EDIT: Now I know why I was mislead, what never compiles in AGS is the logical negation "if (!music_channel)", not even when it's on it's own.

Baron

See, we're all learning here! :)

I think I did check to see if music_check was null at first (as per the manual), but since it wasn't working I took the check out (since I knew it shouldn't have been null).  All part and parcel of the problem solving process. ;)

It seems I misunderstood the manual entry for storing the channel on which the sound is being played as doing only that, rather than playing the track as well.  Rereading it now I see my error, and now I have full control over my music volume!

Many Thanks!

SMF spam blocked by CleanTalk