Author Topic: [SOLVED] Random Songs throughout the game?  (Read 321 times)

[SOLVED] Random Songs throughout the game?
« on: 17 Nov 2021, 19:54 »
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!  :)
« Last Edit: 26 Nov 2021, 18:36 by Rik_Vargard »

Re: Random Songs throughout the game?
« Reply #1 on: 17 Nov 2021, 22:22 »
You need something like:

Code: Adventure Game Studio
  1. // above repeatedly_execute()
  2. AudioChannel* globalBGM;
  3.  
  4.   // inside repeatedly_execute()
  5.   if (globalBGM == null || !globalBGM.IsPlaying) {
  6.     // music has just stopped
  7.     int i = Random(3);
  8.     if (i == 0) globalBGM = aBGM1.Play();
  9.     if (i == 1) globalBGM = aBGM2.Play();
  10.     if (i == 2) globalBGM = aBGM3.Play();
  11.     if (i == 3) globalBGM = aBGM4.Play();
  12.   }

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

edit: code fixed, thanks CW!
« Last Edit: 18 Nov 2021, 23:26 by Khris »

Re: Random Songs throughout the game?
« Reply #2 on: 18 Nov 2021, 15:40 »
Hey thank you so much for the reply!

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

Code: [Select]
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...

Re: Random Songs throughout the game?
« Reply #3 on: 18 Nov 2021, 19:26 »
You also need to test whether globalBGM is null, because it will start as null:

Code: Adventure Game Studio
  1. if ((globalBGM == null) || !globalBGM.IsPlaying)
  2.  
this is saying: do if globalBGM was not assigned OR was assigned but not playing anymore.

Re: Random Songs throughout the game?
« Reply #4 on: 24 Nov 2021, 09:02 »
AHA !!! Thank you very much!!

Re: Random Songs throughout the game?
« Reply #5 on: 25 Nov 2021, 17:02 »
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: Adventure Game Studio
  1.  
  2. function repeatedly_execute()
  3. {
  4.     if ((Channel == null) || !Channel.IsPlaying)
  5.     {
  6.     int i = Random(2);
  7.     if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);
  8.     if (i == 1) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);
  9.     if (i == 2) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);
  10.     }
  11. }  


Now to find out how to make it so it won't repeat twice the same song that just played  :-D

Cassiebsg

  • Cavefish
  • Fleeing the Cylon tyrrany...
    • Cassiebsg worked on one or more games that won an AGS Award!
    •  
    • Cassiebsg worked on one or more games that was nominated for an AGS Award!
Re: Random Songs throughout the game?
« Reply #6 on: 25 Nov 2021, 17:17 »
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).

There are those who believe that life here began out there...

Re: Random Songs throughout the game?
« Reply #7 on: 26 Nov 2021, 17:04 »
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: Adventure Game Studio
  1. function repeatedly_execute()
  2. {
  3.   if (SongNumber == 0)
  4.   {
  5.     if ((Channel == null) || !Channel.IsPlaying)
  6.     {
  7.       int i = Random(2);
  8.       if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);SongNumber = 1;
  9.       if (i == 1) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);SongNumber = 2;
  10.       if (i == 2) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);SongNumber = 3;
  11.     }  
  12.   }
  13.   else if (SongNumber == 1)
  14.   {
  15.     if ((Channel == null) || !Channel.IsPlaying)
  16.     {
  17.       int i = Random(1);
  18.       if (i == 0) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);SongNumber = 2;
  19.       if (i == 1) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);SongNumber = 3;
  20.     }  
  21.   }
  22.   else if (SongNumber == 2)
  23.   {
  24.     if ((Channel == null) || !Channel.IsPlaying)
  25.     {
  26.       int i = Random(1);
  27.       if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);SongNumber = 1;
  28.       if (i == 1) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);SongNumber = 3;
  29.     }
  30.   }
  31.   else if (SongNumber == 3)
  32.   {
  33.     if ((Channel == null) || !Channel.IsPlaying)
  34.     {
  35.       int i = Random(1);
  36.       if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);SongNumber = 1;
  37.       if (i == 1) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);SongNumber = 2;
  38.     }  
  39.   }
  40. }  

Matti

Re: Random Songs throughout the game?
« Reply #8 on: 26 Nov 2021, 17:35 »
Do it like this:

Code: Adventure Game Studio
  1. int PreviousSong = -1; // in the global variable panel or at the top of the script
  2.  
  3.     function repeatedly_execute()
  4.     {
  5.         if ((Channel == null) || !Channel.IsPlaying)
  6.         {
  7.         int i = Random(2);
  8.         while (i == PreviousSong) i = Random(2);
  9.         PreviousSong = i;
  10.         if (i == 0) Channel = aIntro01.Play(eAudioPriorityNormal, eOnce);
  11.         if (i == 1) Channel = aIntro02.Play(eAudioPriorityNormal, eOnce);
  12.         if (i == 2) Channel = aIntro03.Play(eAudioPriorityNormal, eOnce);
  13.         }
  14.     }  
  15.  

Re: Random Songs throughout the game?
« Reply #9 on: 26 Nov 2021, 18:35 »
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

Matti

Re: [SOLVED] Random Songs throughout the game?
« Reply #10 on: 26 Nov 2021, 22:18 »
Yes, that is exactly how it works.

Glad to be of help :)