Game Maker – Basic Looping Background Music Player

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:

track_loop

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:

  1. Combining all your tracks into one long track with a program like Audacity, then play that and loop it.
  2. Running a queue of songs using Game Makers alarms, then looping it.
  3. 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:

OK the first line makes sense, it starts the loop, but what’s with that counter? Good question.

Improving Run-Time

Case 1

Consider the follow diagram of case 1, where we have four tracks:

track_loop_polynomial

Track 1 requires a number of logic checks of whether other tracks are playing or not. These correspond to the orange, purple, yellow and green arrows which represents a check of the track that it is pointing to.

Why is the orange arrow is pointing to itself, is that right? Yes its required to prevent a track re-playing itself while its already playing.

As a general rule with this system the addition of one track brings the addition of a set of one more checks. So in the above case each track must perform four checks, if we had n tracks each track would need to perform n checks.

Note – The above general rule applies to all cases except the start of the loop, which in our music player is playing track 1 in our objects create event.

Performance wise this system is pretty poor as we have n x n (n^2) number of checks, giving us a polynomial run time. We can do better than that, and the track pointer will help us do it.

Case 2

Consider the following case. It is the same situation with the same number of tracks:

track_loop_simplified

Notice how it has less arrows? The grey arrow is the track pointer and with it we can record what was just played, giving the system a form of memory. The only checks that need to be performed now are what track was last played by checking the pointers track number, whether the last track is still playing and if the next track is already playing.

In Case 2 the track pointer is  4, one check is whether track 4 is still playing and the other is if track 1 is already playing.

Now with the addition of another track rather than an n number of checks for the nth additional track we only increase the complexity of the system by a fixed three checks. Much better.

Now to code the above, give our object a step event and include this example code:

I have given my sound track assets names like sound_track_1, be sure to change the these names if you’ve used something different.

That’s it folks, our looping background music player is complete! To see a working implementation of this looping background music system, checkout my game Multi!

Hope this helped, and if you have any questions or problems don’t hesitate to post a comment.

Leave a Reply

Your email address will not be published.

20 − 6 =