Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Rik_Vargard on Wed 17/11/2021 19:54:12

Title: [SOLVED] Random Songs throughout the game?
Post by: Rik_Vargard on Wed 17/11/2021 19:54:12
Hiya!

So, I'm out of options, research and ideas...

What I'd like to do is having some kind of global script thingy or something that would play "global music" while playing through the game whatever the room you're in...
BUT I'd like the new song to be random every time a song ends.

By now I know how to create a channel and have it play a random song but it's always the same song repeating once done.

I know the different song lengths might be a problem, but I don't care having a moment without music based on the longest song if I can work around the Wait() thing that stops everything.
The Wait() idea was to create a "non-blocking" wait time based on the longest song and then it would choose a random song again.

I tried some stuff in the global script repeatedly_execute_always() But it's a mess  :-D

Any ideas?

Thanks in advance!  :)
Title: Re: Random Songs throughout the game?
Post by: Khris on Wed 17/11/2021 22:22:46
You need something like:

Code (ags) Select
// above repeatedly_execute()
AudioChannel* globalBGM;

  // inside repeatedly_execute()
  if (globalBGM == null || !globalBGM.IsPlaying) {
    // music has just stopped
    int i = Random(3);
    if (i == 0) globalBGM = aBGM1.Play();
    if (i == 1) globalBGM = aBGM2.Play();
    if (i == 2) globalBGM = aBGM3.Play();
    if (i == 3) globalBGM = aBGM4.Play();
  }


I'm pretty sure that's all you need.

edit: code fixed, thanks CW!
Title: Re: Random Songs throughout the game?
Post by: Rik_Vargard on Thu 18/11/2021 15:40:27
Hey thank you so much for the reply!

So I think I did what you told me to do:


AudioChannel* globalBGM;
function repeatedly_execute()
{
if (!globalBGM.IsPlaying) {
    int i = Random(1);
    if (i == 0) globalBGM = aIntro02.Play();
    if (i == 1) globalBGM = aIntro03.Play();
  }
}


I put it in the global script.
But it doesn't work; the error message says "Error: Null pointer referenced"

So I tried some stuff, tried replacing "*" with a channel name, went to the manual, didn't find anything, can't make it work...
Title: Re: Random Songs throughout the game?
Post by: Crimson Wizard on Thu 18/11/2021 19:26:49
You also need to test whether globalBGM is null, because it will start as null:

Code (ags) Select

if ((globalBGM == null) || !globalBGM.IsPlaying)

this is saying: do if globalBGM was not assigned OR was assigned but not playing anymore.
Title: Re: Random Songs throughout the game?
Post by: Rik_Vargard on Wed 24/11/2021 09:02:01
AHA !!! Thank you very much!!
Title: Re: Random Songs throughout the game?
Post by: Rik_Vargard on Thu 25/11/2021 17:02:10
OK! So I finally discovered how to make sure it won't play that same song over and over again once the thing is launched.
I also made Global variable called "Channel" as "Audiochannel* type.

Right now I have this :

Code (ags) Select


function repeatedly_execute()
{
    if ((Channel == null) || !Channel.IsPlaying)
    {
    int i = Random(2);
    if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);
    if (i == 1) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);
    if (i == 2) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);
    }
}   



Now to find out how to make it so it won't repeat twice the same song that just played  :-D
Title: Re: Random Songs throughout the game?
Post by: Cassiebsg on Thu 25/11/2021 17:17:22
Create an int and change it for each song. Then just check if it matches the new random, if it does, run the random again until it doesn't  (use a while or for).

Title: Re: Random Songs throughout the game?
Post by: Rik_Vargard on Fri 26/11/2021 17:04:15
Quote from: Cassiebsg on Thu 25/11/2021 17:17:22
Create an int and change it for each song. Then just check if it matches the new random, if it does, run the random again until it doesn't  (use a while or for).

I will try to understand what you said and try to figure something out.

As a beginner, I barely understand the if thing, so I tried this but it didn't work.
(I created a global variable named SongNumber)

Code (ags) Select

function repeatedly_execute()
{
  if (SongNumber == 0)
  {
    if ((Channel == null) || !Channel.IsPlaying)
    {
      int i = Random(2);
      if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);SongNumber = 1;
      if (i == 1) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);SongNumber = 2;
      if (i == 2) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);SongNumber = 3;
    } 
  }
  else if (SongNumber == 1)
  {
    if ((Channel == null) || !Channel.IsPlaying)
    {
      int i = Random(1);
      if (i == 0) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);SongNumber = 2;
      if (i == 1) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);SongNumber = 3;
    } 
  }
  else if (SongNumber == 2)
  {
    if ((Channel == null) || !Channel.IsPlaying)
    {
      int i = Random(1);
      if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);SongNumber = 1;
      if (i == 1) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);SongNumber = 3;
    }
  }
  else if (SongNumber == 3)
  {
    if ((Channel == null) || !Channel.IsPlaying)
    {
      int i = Random(1);
      if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);SongNumber = 1;
      if (i == 1) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);SongNumber = 2;
    } 
  }
}   
Title: Re: Random Songs throughout the game?
Post by: Matti on Fri 26/11/2021 17:35:22
Do it like this:

Code (ags) Select

int PreviousSong = -1; // in the global variable panel or at the top of the script

    function repeatedly_execute()
    {
        if ((Channel == null) || !Channel.IsPlaying)
        {
        int i = Random(2);
        while (i == PreviousSong) i = Random(2);
        PreviousSong = i;
        if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);
        if (i == 1) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);
        if (i == 2) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);
        }
    }   

Title: Re: Random Songs throughout the game?
Post by: Rik_Vargard on Fri 26/11/2021 18:35:51
So if I'm getting what you're teaching me here is right:

PreviousSong is -1 because random starts at 0
So let's say i = 1 after random
Now PreviousSong becomes i which is 1
So now while randomizing again, while i is PreviousSong thus 1 it will keep randomizing

This is like magic to me, like writing a spell and boom!
Incredible.


Thank you all so much for your time and kindness

Cheers
Title: Re: [SOLVED] Random Songs throughout the game?
Post by: Matti on Fri 26/11/2021 22:18:08
Yes, that is exactly how it works.

Glad to be of help :)