Issues with PlayMusicQueued [SOLVED]

Started by Grundislav, Sat 24/06/2017 15:14:11

Previous topic - Next topic

Grundislav

Hello, I seem to be running into a problem when using PlayMusicQueued, namely that my audio channel seems to become null when I line up music to play.

I've set a global variable Audiochannel called "CurrentMusic." Normally I just use it to play music when entering a room, and check that it's playing to avoid it restarting when I enter the room again, like so:
Code: ags

if (CurrentMusic == null || CurrentMusic.PlayingClip != aFlowershop)
  {
    CurrentMusic = aFlowershop.Play();
  }


In a few instances, I have two tracks which are cut up into an intro section, and a main loop. For these I use PlayMusicQueued. An example of this is when the game starts, I have a short stinger for the game's logos, and then a main loop for the title screen. The problem seems to be that whenever I queue up a track, my AudioChannel thinks it's null.

Specifically, what I'm trying to do is allow the player to press Esc to skip the logo screen. The first room with the logos has this code:
Code: ags

CurrentMusic = aLogoSting.Play();
CurrentMusic = aTitle_Loop.PlayQueued();


As it is, when pressing Esc, it cuts to the title screen, but aLogoSting still plays to completion, and then aTitle_Loop begins. I'd like aTitle_Loop to start playing if the player skips ahead. My thought was that in the title screen, I could say
Code: ags

if (CurrentMusic.PlayingClip == aLogoSting)
{
    aTitle_Loop.Play
}


But instead, I get an error saying that a null pointer is referenced. Why does this happen? And is there any way to do what I'm trying to do?

Crimson Wizard

#1
When PlayQueue cannot play right away, it returns null pointer, because it cannot know which channel it will play on later.

Audio system in AGS cannot "reserve" particular channels due the way it works: instead it may take any channel from available ones and run clip on it. MaxChannels property that you may set up for the AudioType defines the NUMBER of them, but not exact IDs of channels.

PS. A while ago we had several sequential discussions about audio system quirks, I will post the links to my older posts here just for reference, in case there are other questions on it:
http://www.adventuregamestudio.co.uk/forums/index.php?topic=54062.msg636546051#msg636546051
http://www.adventuregamestudio.co.uk/forums/index.php?topic=54188.msg636548639#msg636548639

Grundislav

So basically there's no real way of doing it. I tried the workaround suggested in the other thread, but it didn't work.

Crimson Wizard

#3
Quote from: Grundislav on Sat 24/06/2017 17:25:45
So basically there's no real way of doing it. I tried the workaround suggested in the other thread, but it didn't work.

Which workaround are you refering to?

I think common workaround is to iterate all available channels (there is 8 max of them) to find out if any is playing the music in question.
Keeping global audiochannel pointer may not be a good idea anyway, for the reasons I am explaining in the linked post: you may get into situation when something else (which you do not expect) is playing on it.

Snarky

Quote from: Crimson Wizard on Sat 24/06/2017 16:46:51
When PlayQueue cannot play right away, it returns null pointer, because it cannot know which channel it will play on later.

Audio system in AGS cannot "reserve" particular channels due the way it works: instead it may take any channel from available ones and run clip on it. MaxChannels property that you may set up for the AudioType defines the NUMBER of them, but not exact IDs of channels.

However, when I experimented with it, it seemed to me that for any audio type that has MaxChannels set to some value other than 0, AGS will in fact reserve a range of AudioChannel IDs which will always be used. Which means that if MaxChannels is 1, all music will play on the same AudioChannel. So you only need to populate the CurrentMusic variable once (to a non-zero value). In effect, it should work just by changing it to:

Code: ags
    CurrentMusic = aLogoSting.Play();
    aTitle_Loop.PlayQueued();


However, I'm not sure whether the reserved audio channel can change between game launches, so you might want to be careful with game saves/restores.

Grundislav

Snarky, you're a genius! That worked perfectly. Coupled with Game.SkippingCutscene this solves all my problems! Thanks CW for your help too.

SMF spam blocked by CleanTalk