How to copy become Nintendo and making your games cooler

- - - What's this? - - -

Usually when you play games, you listen music in the background right? Unless you're playing an Atari 2600 game, music has been a very important part of gaming. But sometimes developers take it a step further, mostly Nintendo. What they do is to change details of the music right on the go depending in events happening in the game.

For example, in 1990's Super Mario World, each time you ride Yoshi a drum track plays in sync with the music, adding more depth to the game and making the experience different compared to just walking.

Here is an example of what we want to achieve.

- - - How to do it - - -

For this we're gonna need Game Maker Studio 2. If you don't have it you can use their free trial here.

You will only require one empty object to do all of this. It's quite simple once you get down to it!

- - - Setting it up - - -

For this tutorial we're going to use 5 sounds, one that is the intro and the other 4 are loop variations. You can add more or remove audios if you want though.

Very important! Make sure that all the audios are the exact same length, or it will de-sync and it will just be a mess, we're going to heavily rely on loops.

- - - Writing the code - - -

We are going to start with the CREATE event, this will start the variables that we'll need

    
pause = false //If the game is paused or not
yoshi = false //If we're riding yoshi
//Plays the intro sound, just one variation needed
audio_play_sound(sndIntro,1,false)
    
  

Now we're going to the STEP event. This will let us change either pause or yoshi to true or false.

    
if keyboard_check_pressed(vk_space)
{
  if pause = false audio_play_sound(sndPause,1,false)
	if pause = true audio_play_sound(sndUnpause,1,false)
	pause = !pause
}
if keyboard_check_pressed(vk_enter) yoshi = !yoshi
    
  

Here's where the fun starts. Right under the code we just wrote, we're going to add this. This will let us know if the intro has stopped playing, and then play the four variations. Right afterwards we're going to get at all times the volume of all 4 sounds.

    
if !audio_is_playing(sndIntro) and !audio_is_playing(sndLoop)
{
	audio_play_sound(sndLoop,1,true)
	audio_play_sound(sndLoopP,1,true)
	audio_play_sound(sndLoopY,1,true)
	audio_play_sound(sndLoopYP,1,true)
}
vol[1] = audio_sound_get_gain(sndLoop)
vol[2] = audio_sound_get_gain(sndLoopP)
vol[3] = audio_sound_get_gain(sndLoopY)
vol[4] = audio_sound_get_gain(sndLoopYP)
    
  

And to end things off, we're going to change the volume of each depending on the variables from before.

    
if pause = false //If the game is running
{
	if yoshi = false
	{
		vol[1] += (1-vol[1])*0.4
		vol[2] += (0-vol[2])*0.4
		vol[3] += (0-vol[3])*0.4
		vol[4] += (0-vol[4])*0.4
	}
	else if yoshi = true
	{
		vol[1] += (0-vol[1])*0.4
		vol[2] += (0-vol[2])*0.4
		vol[3] += (1-vol[3])*0.4
		vol[4] += (0-vol[4])*0.4
	}
}
else if pause = true //If the game is running
{
	if yoshi = false
	{
		vol[1] += (0-vol[1])*0.4
		vol[2] += (1-vol[2])*0.4
		vol[3] += (0-vol[3])*0.4
		vol[4] += (0-vol[4])*0.4
	}
	else if yoshi = true
	{
		vol[1] += (0-vol[1])*0.4
		vol[2] += (0-vol[2])*0.4
		vol[3] += (0-vol[3])*0.4
		vol[4] += (1-vol[4])*0.4
	}
}
//Set the volume to the desired values
audio_sound_gain(sndLoop,vol[1],1)
audio_sound_gain(sndLoopP,vol[2],1)
audio_sound_gain(sndLoopY,vol[3],1)
audio_sound_gain(sndLoopYP,vol[4],1)
    
  

And that is it! Congratulations!!! You've made a dynamic sound system!!!

As a side not I want to show one of the Cool Coder Strats™ that I used in the last segment.

    
x+=(y-x)*z
    
  

"X" is the number we want to modify, "Y" is the target we want to get to, and "Z" is the speed of this. It will help you have a smooth curve, instead of an abrupt cut.

I hope that this small tutorial helped you! Credit would be appreciated. Have fun and good luck with your gamedev adventures!

- - - - - -