• All, Gmail is currently rejecting messages from my host. I have a ticket in process, but it may take some time to resolve. Until further notice, do NOT use Gmail for your accounts. You will be unable to receive confirmations and two factor messages to login.

random music

Yeah sorry mine wasn't a proper solution, I was trying to show him what he had to edit.

Your script is much smarter.  Is there a way to add it to a level as script instead?  I couldn't make it work without the frame check.
 
Is there a way to add it to a level as script instead?

Depends how levelscript are handled. Rand() has a bug (not OpenBOR fault, its C fault) that the random seed is updated just 1 time per second, not by each time you call it. So, if you call rand() two times on the with less than 1 second between, you will get the same value.

This SHOULD work as a level script, I don't see why it should not.
 
I mean to add it to a spawn in a level the way I'm using this script for sfx.

Code:
spawn empty
@script
void main()
{
  int SFX = loadsample("data/sfx/01.wav");
  playsample(SFX, 0, 120, 120, 100, 0);
}
@end_script
coords 0 654
at 640
 
You can try this:

spawn empty
@script
void main()
{
  int r = rand()%5+5;
  switch(r)
  {
    case 0:
    playmusic("data/music/0.ogg", 1);
    break;
    case 1:
    playmusic("data/music/1.ogg", 1);
    break;
    case 2:
    playmusic("data/music/2.ogg", 1);
    break;
    case 3:
    playmusic("data/music/3.ogg", 1);
    break;
    case 4:
    playmusic("data/music/4.ogg", 1);
    break;
    case 5:
    playmusic("data/music/5.ogg", 1);
    break;
    case 6:
    playmusic("data/music/6.ogg", 1);
    break;
    case 7:
    playmusic("data/music/7.ogg", 1);
    break;
    case 8:
    playmusic("data/music/8.ogg", 1);
    break;
    case 9:
    playmusic("data/music/9.ogg", 1);
    break;
    case 10:
    playmusic("data/music/10.ogg", 1);
    break;
    default:
    playmusic("data/music/0.ogg", 1);
    break;
  }
}
@end_script
coords 0 654
at 0

I modified Ilu's first example which is more flexible
 
Ummm... not with this kind of script. For random enemies, I have 2 ways which I have done (not theory anymore :) ):
1. Use entity which spawns random enemy
2. Use spawn script to remove or let enemies be spawned in a level

#1 is great to spawn random enemies from same spot
#2 is great for random enemies coming from different spots
 
I said it's flexible cause it allows modders to set name of music to each line directly. Changing music names to value means extra work and it would be troublesome to remember which number is which song.
 
Just a basic animationscript for randommusic I made,

Usage:

3 TRACKS
Code:
@cmd randMusic "1.ogg" "2.ogg" "3.ogg"

6 TRACKS
Code:
@cmd randMusic2 "1.ogg" "2.ogg" "3.ogg" "4.ogg" "5.ogg" "6.ogg"


randmusic.c
Code:
void randMusic(char s1,char s2,char s3)
{
   int r = rand()%300;
   char mus;
   if (r<=-100)
   {
      mus=s1;
   }else if(r<=100){
      mus=s2;
   }else{
      mus=s3;
   }
   playmusic("data/music/"+mus, 1);
}
void randMusic2(char s1,char s2,char s3,char s4,char s5,char s6)
{
   int      r      =   rand()%300+300;
   char   mus   =   "";

   if (r<100){
      mus=s1;
   }else if(r<200){
      mus=s2;
   }else if(r<300){
      mus=s3;
   }else if(r<400){
      mus=s4;
   }else if(r<500){
      mus=s5;
   }else if(r<600){
      mus=s6;
   }
   playmusic("data/music/"+mus, 1);
}
 
Hmmmm... though I like the look of that randmusic script you shared BeasTie, I don't know if that could work. I will try to test to see how it works. BTW the looping of music which is similar to utunnels's way just popped up to my mind now. Some operators could work for music like the way he did.
 
I wouldn't post it if it didn't work  ::)

It's an OLD script, there's other solutions posted already.  But I still use it sometimes, it's for random stage music, so looping is always ON ;)

 
Tested and it works. I create a new txt as empty for music. :)

Code:
name Random_Musica
type none
speed 8
animationscript data/scripts/randmusic.c


anim	idle
	offset	1 1
	delay	2
	loop 0
@cmd randMusic2 "ryu.ogg" "ken.ogg" "joe2.ogg" "cody1.ogg" "balrog.ogg" "bison.ogg"
	frame	data/chars/misc/empty.gif

Bloodbane's way does work well similarly too. :)

It's really awesome to use random music in either BeasTie's way or Bloodbane's way. Both work good. Great job, you two. Cheers!

EDIT: Hmm.. I am thinking about switching random music in DC's way. You know, switching to different tracks. I'd like one track to finish and then another track play. :)
 
How can I know about the elapsed time for the length of each music before switching or randomizing it like a music shuffle? For example, if one track is 2:49, do I need to make it elapsed_time,16781 (centiseconds)? I'm lost in

I test-play the music like this in the stage similar to your way, Bloodbane.
Code:
spawn empty
@script
void main()
{
  int r = rand()%5+5;
  switch(r)
  {
    case 0:
    playmusic("data/music/ryu.ogg", 1);
    break;
    case 1:
    playmusic("data/music/ken.ogg", 1);
    break;
    case 2:
    playmusic("data/music/balrog.ogg", 1);
    break;
    case 3:
    playmusic("data/music/bison.ogg", 1);
    break;
    case 4:
    playmusic("data/music/cody1.ogg", 1);
    break;
    case 5:
    playmusic("data/music/joe1.ogg", 1);
    break;
    case 6:
    playmusic("data/music/akuma.ogg", 1);
    break;
    case 7:
    playmusic("data/music/gou-ryu.ogg", 1);
    break;
    case 8:
    playmusic("data/music/cody.ogg", 1);
    break;
    case 9:
    playmusic("data/music/joe.ogg", 1);
    break;
    case 10:
    playmusic("data/music/intro.ogg", 1);
    break;
    default:
    playmusic("data/music/ryu.ogg", 1);
    break;
  }
}
@end_script
coords 0 654
at 0

BTW what do 'case' and 'break' mean?
 
Instead of using level script, why not use entity for this purpose?
Something like:
name Randommusic
type none

anim idle
@script
    if(frame==1){
      playmusic("data/music/ken.ogg", 1);
    }
    if(frame==2){
      void self = getlocalvar("self");
      int r = rand()%30;

      if( r > 10 ){
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW1"));
      } else if( r < -10 ){
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW2"));
      }
    }
@end_script
loop 1
delay 1
offset 1 1
frame data/chars/misc/empty.gif
delay 20000
frame data/chars/misc/empty.gif
delay 1
frame data/chars/misc/empty.gif

anim follow1
@script
    if(frame==1){
      playmusic("data/music/bison.ogg", 1);
    }
    if(frame==2){
      void self = getlocalvar("self");
      int r = rand()%30;

      if( r > 10 ){
        setidle(self, openborconstant("ANI_IDLE"));
      } else if( r < -10 ){
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW2"));
      }
    }
@end_script
loop 1
delay 1
offset 1 1
frame data/chars/misc/empty.gif
delay 25000
frame data/chars/misc/empty.gif
delay 1
frame data/chars/misc/empty.gif

anim follow2
@script
    if(frame==1){
      playmusic("data/music/ryu.ogg", 1);
    }
    if(frame==2){
      void self = getlocalvar("self");
      int r = rand()%30;

      if( r > 10 ){
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW1"));
      } else if( r < -10 ){
        setidle(self, openborconstant("ANI_IDLE"));
      }
    }
@end_script
loop 1
delay 1
offset 1 1
frame data/chars/misc/empty.gif
delay 30000
frame data/chars/misc/empty.gif
delay 1
frame data/chars/misc/empty.gif

In this example, each animation plays a music and has its own delay time before it plays the next random music. You can adjust delay value to suit music's length
As you can see this example only features 3 tracks, you can extend it for more tracks
 
BTW what do 'case' and 'break' mean?

"Case" is used when we have a "Switch". Switch works like a sequence of "if...else" nested. Instead of having 5, 6 cases of  "if...else if", you should use Switch.

switch_statement.jpg


Each "case" is a condition. The BREAK is used to, as the name implies, to break the logic lasso or to stop the action.

You can read a way better explanation here: http://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm

I don't remember if OpenBOR script accepts DEFAULT. If not, you should create a CASE for that because its mandatory. You need to have at least one valid condition in case of something bad happen or all the CASES are false.

Just a side note:
"break"is not THAT optional. Sure, if the function finds a valid CASE (and you will have just one valid case), it will process just that CASE and stops. But all the other CASE will be still tested every tick. So, its better to use a BREAK after all CASE to make it stop.
 
O Ilusionista said:
BTW what do 'case' and 'break' mean?
I don't remember if OpenBOR script accepts DEFAULT. If not, you should create a CASE for that because its mandatory. You need to have at least one valid condition in case of something bad happen or all the CASES are false.

Default is valid in OpenBOR.

DC
 
Thanks O and DC! ^_^ Though I figured about case for the order number and break at the end of the argument (but didn't see the Switch part) before you (O Ilusionista) posted, I appreciate it. After learning what Switch does from you and the link, that's when I realized that Bloodbane mentioned 'switch' before the argument.

Honestly I never take C programming class before, but doing C-based programming for OpenBOR is so fantastic/awesome! :D It's great that I don't have to be an expert on C programming for working on C based stuff for OpenBOR, so I cannot be too hyped or excited about it with no skill. Anyone with any level can do this especially those with lower level. I never learned scripts before, but since I'm learning scripts, it is very interesting to do.
 
Back
Top Bottom