• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] CG's Music System

Level 14
Joined
Nov 20, 2005
Messages
1,156
Music is, to put it bluntly, a bit messed up and tricky to work in WC3. So here's my music system, taken out of Destiny's Wake (with a few alterations, mainly taking out the need for Vex's CS safety). Obviously it still needs vJASS. Will store where it is in the track

JASS:
library MusicSystem initializer Setup_Music

globals
    private string current = null
    private timer t
    constant string FIGHT_MUSIC = "3circles.mp3"
    constant string NORMAL_MUSIC = "Medieval.mp3"
    private real fightTime = 0.
    private real normalTime = 0.
endglobals

// These must have the data of the various songs you use.
private function SetMusicRestart takes string s returns nothing
    if s == FIGHT_MUSIC then
        set fightTime = ModuloReal(fightTime + TimerGetElapsed(t), 620.)
    
    elseif s == NORMAL_MUSIC then
        set normalTime = ModuloReal(normalTime + TimerGetElapsed(t), 245.)
    
    endif
endfunction

// This must have the correct returns for the various songs you use.
private function GetMusicRestart takes string s returns real
    if s == FIGHT_MUSIC then
        return fightTime
    elseif s == NORMAL_MUSIC then
        return normalTime
    else
        return 0.
    endif
endfunction

private function StartMusicEx takes nothing returns nothing
    call StopMusic(false)
    if current != null then
        call PlayMusic(current)
        call SetMusicPlayPosition( R2I( GetMusicRestart(current)*1000 ) )
        call TimerStart(t, 99999., false, null)
    endif
    call DestroyTimer(GetExpiredTimer())
endfunction

function StartMusic takes string s returns nothing
    call ClearMapMusic()
    if s != null then
        call SetMapMusic(s, false, 0)
    endif
    if current != null then
        call StopMusic(true)
        call SetMusicRestart(current)
        if s != null then
            call TimerStart(CreateTimer(), 2., false, function StartMusicEx) // This time is to allow time for fade out. Reduce it to make the new music come in faster.
        endif
    else
        call StopMusic(false)
        if s != null then
            call PlayMusic(s)
            call SetMusicPlayPosition( R2I( GetMusicRestart(s)*1000 ) )
            call TimerStart(t, 99999., false, null)
        endif
    endif
    set current = s
endfunction

private function Setup_Music takes nothing returns nothing
    set t = CreateTimer()
endfunction

endlibrary

How to use it? Quite simple. Set up the two functions which I've commented, and the necessary globals, and then whever you want to use it, do this:

JASS:
    call StartMusic(NORMAL_MUSIC) //this will play the normal music variable
    call StartMusic(null) // This will stop the music
 
Last edited:
Top