[SOLVED] Random Songs throughout the game?

Started by Rik_Vargard, Wed 17/11/2021 19:54:12

Previous topic - Next topic

Rik_Vargard

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!  :)

Khris

#1
You need something like:

Code: ags
// 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!

Rik_Vargard

Hey thank you so much for the reply!

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

Code: ags

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...

Crimson Wizard

You also need to test whether globalBGM is null, because it will start as null:

Code: ags

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

this is saying: do if globalBGM was not assigned OR was assigned but not playing anymore.

Rik_Vargard


Rik_Vargard

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


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

Cassiebsg

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...

Rik_Vargard

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

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;
    }  
  }
}   

Matti

Do it like this:

Code: ags

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);
        }
    }   


Rik_Vargard

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

Yes, that is exactly how it works.

Glad to be of help :)

SMF spam blocked by CleanTalk