random music

Probably a few ways, but you can just have an entity that spawns and chooses random music.

http://www.chronocrash.com/forum/index.php?topic=653.msg4931#msg4931

@Bloodbane - something like this would work? Or add similar script in the stage itself on a spawn command?

Code:
      @script
    void self = getlocalvar("self");
    if( frame == 0){
      int r = rand()%30;
      if( r > 11){
      playmusic("data/music/music1.ogg", 0);
      } else if( r < -10){
        playmusic("data/music/music2.ogg", 0);
      } else if( r < 0){
        playmusic("data/music/music3.ogg", 0);
      } else if( r > 0){
        playmusic("data/music/music4.ogg", 0);
      }
    }
   @end_script
 
That would work
However I can't tell the best way yet until Jiam specifies the above. After he do that, we could decide whether it's best to use entity or just randomize music with script in level directly
 
Slightly offtopic, it's easy now to do random music or change music.  But openbor lacks any way to set NEXT MUSIC

This would be something good to have, even if it's only a scriptable command. 
Best to have engine to decide it I figure because it knows when a track is playing and finished, instead of looping or stopping it plays nextmusic.

ideally thou, just hardcoded like this -
Code:
music data/music/music1.ogg 0
nextmusic data/music/music2.ogg 1
 
That will be fine, we just make the song loop once it chooses which track to play.

Just change the 0 to 1
Code:
playmusic("data/music/music2.ogg", 0);

1. how many tracks thou?

2.  You want each stage to choose from all available music, like a big playlist?, or each stage has it's own small selection of (like 3-4) tracks to choose from?

If it's a few tracks per stage, then something like I did will be fine, but if you want a big random playlist for every stage than you will probably need a better random code than that.
 
Just increase the script to 10 tracks, but change the random values to suit 10.

You'll have to edit this, but it's the basic entity you would spawn at start of level.

Code:
name  randMusic
type    none
gfxshadow 0

anim    idle
loop    0
offset 0 0
    @script
    void self = getlocalvar("self");
    if( frame == 0){
      int r = rand()%30;
      if( r > 11){
      playmusic("data/music/track1.ogg", 1);
      } else if( r < -10){
        playmusic("data/music/track2.ogg", 1);
      } else if( r < 0){
        playmusic("data/music/track3.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/track4.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/track5.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/track6.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/track7.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/track8.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/track9.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/track10.ogg", 1);
      }
    }
   @end_script
frame       none
frame       none
 
? not

this is what I've done:
Code:
name	musica_al_azar
type    none
gfxshadow 0

anim    idle
    loop    0
    offset  0 0

    @script
    void self = getlocalvar("self");
    if( frame == 0){
      int r = rand()%30;
      if( r > 11){
        playmusic("data/music/1.ogg", 1);
      } else if( r < -10){
        playmusic("data/music/2.ogg", 1);
      } else if( r < 0){
        playmusic("data/music/3.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/4.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/5.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/6.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/7.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/8.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/9.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/10.ogg", 1);
      }
    }
   @end_script
    		
frame  none
frame  none
 
There is an error on that code, look:

@script
    void self = getlocalvar("self");
    if( frame == 0){
      int r = rand()%30;
      if( r > 11){
        playmusic("data/music/1.ogg", 1);
      } else if( r < -10){
        playmusic("data/music/2.ogg", 1);
    } else if( r < 0){
        playmusic("data/music/3.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/4.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/5.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/6.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/7.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/8.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/9.ogg", 1);
      } else if( r > 0){
        playmusic("data/music/10.ogg", 1);

      }
    }
  @end_script

All the options have the same trigger (r>0), but each option should have different trigger (values).
Plus, if you use IF, ELSE IF you should finish with ELSE.

If its 10 musics, then use a random with a value of 10 and check. And I suggest you to use SWITCH instead of IF
 
@script
    void self = getlocalvar("self");
    if( frame == 0){
int r = rand()%10+10;
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

Or I think you can do even better:

@script
    void self = getlocalvar("self");
    if( frame == 0){
      int r = rand()%10+10;
      playmusic("data/music/"+r+".ogg", 1);
}
    }
  @end_script

I haven't tested it yet.
 
Back
Top Bottom