Basic Music Randomizer

Post your example zscripts/ACS scripts/etc here.
Forum rules
The Projects forums are only for projects. If you are asking questions about a project, either find that project's thread, or start a thread in the General section instead.

Got a cool project idea but nothing else? Put it in the project ideas thread instead!

Projects for any Doom-based engine (especially 3DGE) are perfectly acceptable here too.

Please read the full rules for more details.
User avatar
Sir Robin
Posts: 392
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Basic Music Randomizer

Post by Sir Robin »

There are probably fancier music handler mods out there, but I threw this together to solve one issue - When I'm testing my levels I usually test them in Doom2 as MAP01, and I always hear the same song over and over again. So this code will change to a random level music every time a level is loaded.

It works by first generating a list of available music by iterating levelinfo nodes and then picking one at random.

Here's the code:

Code: Select all

class RandomMusicPlayer : eventhandler
{
    string MusicSelection;

    override void OnRegister()
    {
        ChangeMusicRandom();
    }

    void ChangeMusicRandom()
    {
        if (!MusicSelection) MusicSelection = GetRandomMusicLump();
        if (MusicSelection)
        {
            console.printf("RandomMusicPlayer: Playing music: "..MusicSelection);
            S_ChangeMusic(MusicSelection);
        }
        else
        {
            console.printf("RandomMusicPlayer: No valid music lumps found.");
        }
    }
    
    string GetRandomMusicLump()
    {
        array<string> MusicList;

        int LevelInfosCount=levelinfo.GetLevelInfoCount();
        for (int i = 0; i < LevelInfosCount; i++)
        {
            levelinfo li = levelinfo.GetLevelInfo(i);
            string musicLump=li.music;
            
            // because of a quirk in how Doom/2 music is handled
            if (musicLump.left(1) == "$") musicLump="D_"..stringtable.localize(musicLump);
            
            if (MusicLump && wads.findlump(MusicLump) >= 0)
            {
                //console.printf("Music for level "..li.levelnum..": "..li.music.." = "..MusicLump);//debug
                MusicList.push(musicLump);
            }
        }
        
        if (MusicList.size() > 0)
        {
            return MusicList[random(0,MusicList.size() - 1)];
        }
        else
        {
            return "";
        }
    }
}
 
Last edited by Sir Robin on Sat Jul 02, 2022 1:18 am, edited 1 time in total.
User avatar
Sir Robin
Posts: 392
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: Basic Music Randomizer

Post by Sir Robin »

WorldLoaded() is called only when a fresh map is loaded, not for example when a save game is loaded. Moved the music randomizer from WorldLoaded() on OnRegister() which is called every game start. Updated code in original post.
User avatar
Ozymandias81
Posts: 2051
Joined: Thu Jul 04, 2013 8:01 am
Discord: Ozymandias81#2768
Github ID: Ozymandias81
Graphics Processor: nVidia with Vulkan support
Location: Mount Olympus, Mars

Re: Basic Music Randomizer

Post by Ozymandias81 »

While I am not sure if the Wumpus mod has the music randomizer, but it happened a crash while changing the midi device from Fluidsynth to anything, using GZDoom 4.9pre 115. Typing this here in case if this is the "mod" that causes such issue or if it comes from GZDoom itself. Can confirm at least that it doesn't happen with Blade of Agony.

Here you are a screenshot using Developer 8 command (or was 7? Can't remember atm), also I will post here the report for devs in case.
CrashReport.zip
You do not have the required permissions to view the files attached to this post.
User avatar
Sir Robin
Posts: 392
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: Basic Music Randomizer

Post by Sir Robin »

Ozymandias81 wrote:While I am not sure if the Wumpus mod has the music randomizer, but it happened a crash while changing the midi device from Fluidsynth to anything, using GZDoom 4.9pre 115. Typing this here in case if this is the "mod" that causes such issue or if it comes from GZDoom itself. Can confirm at least that it doesn't happen with Blade of Agony.

Here you are a screenshot using Developer 8 command (or was 7? Can't remember atm), also I will post here the report for devs in case.
CrashReport.zip
I haven't tried that version but I don't think my mod could be doing anything like that. It doesn't even load until you load a level, and the screenshot shows you still at the titlescreen

Return to “Script Library”