Have you played a game with no background music/sounds? In most cases it makes the game feel quite dead. So in developing my game Multi I needed a basic looping background music player of sorts which filled the following requirements:
- Tracks needed to be played one after another starting from the first track to some n number of tracks.
- Through this iteration of tracks, tracks must only be played one time.
- On reaching the end of the final track the first track would again be played creating an infinite loop.
For clarity purposes heres a diagram:
Following the blue arrows from Track 1, tracks are listed in play order from left to right then restarting from the last to the first. Put simply I wanted to have a looping background music player. With this in mind, I began the project with some research and found this post on Steam Community.
The basic summary of what’s suggested in the above link is:
- Combining all your tracks into one long track with a program like Audacity, then play that and loop it.
- Running a queue of songs using Game Makers alarms, then looping it.
- Using simple logic based checks, play one song after another then repeat to form a loop.
All the above are valid methods which have advantages and disadvantages, but for the foundation of this music player I went with option three.
Building the Music Player
To start create a persistent object. Name is whatever you want, it doesn’t need a sprite either, but it will be need to be placed in the room you want your music to start in. If this object is not persistent and the music player gets reinitialized at any point, it will lose its place and start from (track 1) the beginning again. For a looping music player this is not what we want.
Now give the persistent object a create event, and add this code:
// Start sound_track_1
audio_play_sound(sound_track_1, 1, false);
// track_pointer initialization
track_pointer = 1;
OK the first line makes sense, it starts the loop, but what’s with that counter? Good question.