tModLoader Custom Daytime/NightTime Music [HELP!]

slade3501

Terrarian
Alright, to make this short, when you start up Terraria, there's either two types of songs that play during DayTime in the normal Terraria biome.
During NightTime, night music plays, got it?

So, basically, I want to add another song that "has a chance to play" during DayTime and NightTime.
So, how exactly would I do this?

Thanks!
 
I just tried this and it seemed to work. Basically, you need to watch for when the music changes, and when it changes to the one you want to be an alternate for, you make a decision whether or not to play the alternate music. There is an extra check with the overworld music since there already are 2 songs and the code will randomize it's choice if it detects that it's not playing, but you should be able to use this same method with providing other alternative sounds.
Code:
        bool chosenAlternateSong = false;
        int previousMusic;

        public override void UpdateMusic(ref int music)
        {
            // If the song just changed....
            if (previousMusic != music)
            {
                // ....and it just changed to an overworld song
                if (music == MusicID.AltOverworldDay || music == MusicID.OverworldDay)
                {
                    // Because of how vanilla chooses alternates, we need to do this as well so the music change persists
                    if (previousMusic != MusicID.OverworldDay && previousMusic != MusicID.AltOverworldDay)
                    {
                        // decide to choose Mod music
                        chosenAlternateSong = (Main.rand.Next(3) == 0);
                    }
                }
                else
                {
                    // Music changed to something else, so don't play anymore.
                    chosenAlternateSong = false;
                }
            }
            // Save previous as
            previousMusic = music;
            // If playing mod music, play it.
            if (chosenAlternateSong)
            {
                music = this.GetSoundSlot(SoundType.Music, "Sounds/Music/DriveMusic");
            }
        }
 
Awesome! I can't wait to try this out!
Except....
This code goes... where? I'm assuming my ModWorld file correct?
Thank you so much! This will be very helpful. ^.^

Edit:
I tried it in ModWorld, but got a "no suitable method found to override" error on compile, this confuses me. Why can't it override UpdateMusic?
[Newbie, still taking Lessons]
 
Last edited:
Awesome! I can't wait to try this out!
Except....
This code goes... where? I'm assuming my ModWorld file correct?
Thank you so much! This will be very helpful. ^.^

Edit:
I tried it in ModWorld, but got a "no suitable method found to override" error on compile, this confuses me. Why can't it override UpdateMusic?
[Newbie, still taking Lessons]
In the class that extends mod. https://github.com/bluemagic123/tModLoader/wiki/Mod#public-virtual-void-updatemusicref-int-music
 
Back
Top Bottom