[Solved] random function tied to length of song playing as a continual loop

Started by theretrodrisk, Fri 15/05/2020 23:59:37

Previous topic - Next topic

theretrodrisk

Hi everyone...

Apologies for another question...

I am trying to do a background Rock Concert in my game.  What I wan't to happen is that in the background the band plays a random tune, says a background say lead in to what the song is... Then play the tune and animate the band.
Then at the end of the song the band stops animating, it does another random selection and does that lead in and new tune.

The bit I am confused with (well a lot actually :) )  is how to only do the random loop once the song has fully played.. I have tried to use a timer (based on searches on here), but getting in a right mess... Typical of me, always going mega complex ha ha :-)

Any help in where I am going wrong is  greatly appreciated.


Code: ags

function room_RepExec()
{
  
SetTimer(1, TimeDelay);
int ran = Random(2);
if (IsTimerExpired(1)) {  
 if (ran == 0){
int TimeDelay=60;
cChuckRock.SayBackground("Right We have a real classic now");
cChuckRock.SayBackground("Yes it's the tune that made us, with our own game");
cChuckRock.SayBackground("Composed originally by the talented Matthew Simmonds ");
cChuckRock.SayBackground("But we are gonna turn it up to eleven, with the");
cChuckRock.SayBackground("Brilliant Aki Järvinen Metal mix...");
cChuckRock.SayBackground("Are you ready to get rocking?");	
cChuckRock.SayBackground("...A One...");
cChuckRock.SayBackground("...A Two.");
cChuckRock.SayBackground("...A One, Two, Three, Four...");
aChuck_Rock_metal_remix.Play();
cChuckRock.Animate(1, 3, eRepeat, eNoBlock, eForwards);
cDinoBand.Animate(1, 3, eRepeat, eNoBlock, eForwards);
cGary.Animate(1, 3, eRepeat, eNoBlock, eForwards);
cOrphBand.Animate(1, 3, eRepeat, eNoBlock, eForwards);
}
else if (ran == 1)
{
cChuckRock.SayBackground("Right We have a real classic now");
cChuckRock.SayBackground("Yes it's the tune that made us, with our own game");
cChuckRock.SayBackground("Composed originally by the talented Matthew Simmonds ");
cChuckRock.SayBackground("But we are gonna turn it up to eleven, with the");
cChuckRock.SayBackground("Brilliant Aki Järvinen Metal mix...");
cChuckRock.SayBackground("Are you ready to get rocking?");	
cChuckRock.SayBackground("...A One...");
cChuckRock.SayBackground("...A Two.");
cChuckRock.SayBackground("...A One, Two, Three, Four...");
aChuck_Rock_metal_remix.Play();
cChuckRock.Animate(1, 3, eRepeat, eNoBlock, eForwards);
cDinoBand.Animate(1, 3, eRepeat, eNoBlock, eForwards);
cGary.Animate(1, 3, eRepeat, eNoBlock, eForwards);
cOrphBand.Animate(1, 3, eRepeat, eNoBlock, eForwards);
}
else if (ran == 2)
{
cChuckRock.SayBackground("Right We have a real classic now");
cChuckRock.SayBackground("Yes it's the tune that made us, with our own game");
cChuckRock.SayBackground("Composed originally by the talented Matthew Simmonds ");
cChuckRock.SayBackground("But we are gonna turn it up to eleven, with the");
cChuckRock.SayBackground("Brilliant Aki Järvinen Metal mix...");
cChuckRock.SayBackground("Are you ready to get rocking?");	
cChuckRock.SayBackground("...A One...");
cChuckRock.SayBackground("...A Two.");
cChuckRock.SayBackground("...A One, Two, Three, Four...");
aChuck_Rock_metal_remix.Play();
cChuckRock.Animate(1, 3, eRepeat, eNoBlock, eForwards);
cDinoBand.Animate(1, 3, eRepeat, eNoBlock, eForwards);
cGary.Animate(1, 3, eRepeat, eNoBlock, eForwards);
cOrphBand.Animate(1, 3, eRepeat, eNoBlock, eForwards);
}
}
}

Khris

That function runs 40 times per second, so it keeps resetting the timer. You also need to declare variables before using them.
Also, you can check the AudioChannel returned by AudipClip.Play() for whether it's still playing something.

Code: ags
int previousSong = -1;
AudioChannel* bgMusic;

function PlayRandomSong() {
  int nextSong = -1;
  while (nextSong == previousSong) nextSong = Random(2);
  AudioClip* clips[3];
  clips[0] = aChuck_Rock_metal_remix;
  clips[1] = aChuck_Rock_metal_remix;
  clips[2] = aChuck_Rock_metal_remix;
  bgMusic = clips[nextSong].Play();
  previousSong = nextSong;
}


In RepExec, check if  bgMusic.Playing == false  and call PlayRandomSong();

Chaining BackgroundSay commands is trickier, there's a dedicated module to do this. Your current code will esssentially run all commands in the same millisecond, with the last one being the only visible one.

theretrodrisk

That's amazing Khris, you are the man.

Thank you so much for all your help... I will look to implement it in my code.

SMF spam blocked by CleanTalk