HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

[Music Problem] How to make it play random music?

03-03-2009, 04:16 PM#1
Alexis_Septimus
Trigger:
Music
Collapse Events
Time - Music[1] expires
Conditions
Collapse Actions
Sound - Play ArthasTheme <gen>
Countdown Timer - Start Music[(Random integer number between 1 and 3)] as a One-shot timer that will expire in 123.00 seconds

Trigger:
Music 2
Collapse Events
Time - Music[2] expires
Conditions
Collapse Actions
Sound - Play BloodElfTheme <gen>
Countdown Timer - Start Music[(Random integer number between 1 and 3)] as a One-shot timer that will expire in 145.00 seconds

Trigger:
Music 3
Collapse Events
Time - Music[3] expires
Conditions
Collapse Actions
Sound - Play Comradeship <gen>
Countdown Timer - Start Music[(Random integer number between 1 and 3)] as a One-shot timer that will expire in 124.00 seconds

I currently trying to figure out how to make the map play a random music without the need of making a multiple trigger this way.

I have browse through the entire tutorial at wc3c, but none of the tutorial cover this part. The tutorial of Play Music by yuripro84 are none accessible as well. Is there any other way to optimize it?
03-03-2009, 04:25 PM#2
Rising_Dusk
That's a very silly approach. What you should do is populate parallel arrays with durations/music paths and then on a single timer executed for <DURATION> seconds, play a random member of the array and re-run the timer for the new <DURATION> number of seconds.
03-03-2009, 04:36 PM#3
Alexis_Septimus
Yeah, I know that trigger wasn't good. I was making this sample that suddenly pop up into my mind.
I was asking a way to optimize it, can you show me the sample of your optimize?

I try use variable, but there is no variable for music.
03-03-2009, 04:44 PM#4
Rising_Dusk
Quote:
Originally Posted by Alexis_Septimus
I try use variable, but there is no variable for music.
It is called 'sound.
Quote:
Originally Posted by Alexis_Septimus
I was asking a way to optimize it, can you show me the sample of your optimize?
I would write it in jass, but I don't have the natives memorized and don't carry common.j with me to work. Sorry. You should be able to easily figure it out from what I said above, though.
03-03-2009, 04:50 PM#5
Michael Peppers
Simply, create a random integer variable, then if its result is 0, set a music and the timer to the duration of that music etc., if its 1, another music etc. and to not make it repeat set a boolean variable that's true when a music has been played, when there's no more music, reset the boolean variables...

Something like this should work, I think

And as Dusk said, using array variables for the various musics, durations (and I say also for the boolean I suggested you) should make this work easier.
03-03-2009, 06:50 PM#6
Alexis_Septimus
Quote:
It is called 'sound.

The problem was. When I try to make a variable out of import music, it doesn't show the list of import music.
03-04-2009, 11:51 AM#7
moyack
I did this small script, but I haven't tested it.

You just have to add the music paths with the function AddTrack("Music\\Path")
Collapse JASS:
library RandomMusic initializer init

globals
    private string array Tracks
    private integer index = 0
    private integer last = 0
endglobals

private function Loop takes nothing returns nothing
    local integer i = GetRandomInt(1, index)
    call PauseTimer(GetExpiredTimer())
    loop // to ensure the music selected is not the same as the last played
        exitwhen index == 1 or i != last
        set i = GetRandomInt(1, index)
    endloop
    set last = i
    call TimerStart(GetExpiredTimer(), I2R(GetSoundFileDuration(Tracks[last])), false, function Loop)
endfunction

private function init takes nothing returns nothing
    if index == 1 then
        set last = GetRandomInt(1, index)
        call TimerStart(CreateTimer(), I2R(GetSoundFileDuration(Tracks[last])), false, function Loop)
    endif
endfunction

function AddTrack takes string soundfile returns nothing
    set Tracks[index] = soundfile
    set index = index + 1
    call init()
endfunction

endlibrary