in , , ,

How To Quickly Set Up 3D Sound In GameMaker

Recent updates to GameMaker Studio added a new audio system to the engine, one that is more code-orientated than previously. To add special effects or utilize 3D sound, developers now have to add a few lines of code to get things working.

Don’t be put off: setting up 3D sound in GameMaker is actually extremely easy.

If you go searching for help on the GameMaker Community forum, it isn’t very easy to find simply-worded documentation on how to achieve this. Fortunately, all you need to know is here.

First we need to give a pair of ears to our player. This is called the ‘listener’ and it lets the game know where we are in the room, and how far we are from active sounds.

In a platform game, the listener could be the character the player controls. In a space game, it could be the space ship. In a game without a controllable player, we might set the listener to be in the middle of the view or the middle of the room, to create directional sound.

To create a listener, we use a simple piece of code in the step event of the object that we want to give ears to:

audio_listener_position(x,y,z);

For those of you that need reminding, if X is left and right, and Y is up and down, Z is forwards and backwards – the Z axis indicates depth (and therefore is more relevant to 3D or fake 3D games).

In a simple 2D game you probably won’t need Z, so you can set it’s value to 0. Set X and Y to the object’s own position. Here is how the listener code might look at this stage:

audio_listener_position(x,y,0);

We now need to create our first sound. Import a sound into your project by dragging it from the desktop and dropping it onto the GameMaker IDE, or by manually creating a new sound asset and importing the file. Edit the sound’s properties in the GameMaker window, so the box to the left is no longer set to ‘Mono’ but instead is set to ‘3D’. Finally, name your sound.

Find (or create) the object you want to play the sound. Add some basic movement code so that you can manipulate the sound’s origin.

Once your object can move, you’ll need to create an ’emitter’ for the sound. Emitters are used for applying real-time audio effects, and are used to position 3D sounds.

In the create event, create the emitter and assign it to a local variable so you can access it later. In the following example, ’emitter_id’ is the name of the variable but you can use a different name.

emitter_id = audio_emitter_create();
audio_play_sound_on(emitter_id,snd_example,true,1);

Creating the emitter is just a matter of calling a single function.

Below this, the sound is played using using a function with four arguments. The first argument contains the emitter identifier that was just assigned. The second argument is the name of the sound you want to play. The third argument is whether or not the sound loops and the fourth argument indicates sound priority. Simply set a high priority for vital sound effects, and a low priority for less important ones.

Your sound will now play, however there is more to do. As the sound-playing object can move, you need to update the emitter’s position to match the object’s position. Use the following code in the step event:

audio_emitter_position(emitter_id,x,y,0);

The first argument contains the emitter identifier. The next two arguments indicate the position to move the emitter to, which should be set to the location of the object that created the emitter. The final argument denotes the Z value which can be set to 0 by default if you don’t require it.

There is one final piece of the puzzle. The emitter should be destroyed when you no longer need it. You can use the following code to achieve this:

audio_emitter_free(emitter_id);

You can add this code in the destroy event if you are deleting an object, or the room end event if you are changing levels. It is important to free your emitter once you no longer need it.

Your game should now play the sound in 3D, appearing distant as the listener object gets further away, and closer as the listener object gets closer. If you find the direction of the sound is not working correctly, use the following line of code in the create event of your listener object.

audio_listener_orientation(lookat_x, lookat_y, lookat_z, up_x, up_y, up_z);

There is no one way to configure this, so refer to the GameMaker help files for further information on how orientation works. For the lazy, you could try something like the following for a quick fix:

audio_listener_orientation(-1000,0,0,0,1,0);

As you can see, it is easy to quickly set up 3D sound in GameMaker. Position the listener, create an emitter, play the sound, and be sure to update the emitter’s location and free it when it’s no longer in use.

What do you think?

6 Comments

Leave a Reply
  1. May I ask how I can set the audio_listener_orientation to the player object’s direction in a 2D-space? I’m absolutely clueless ):

  2. Very nice! I’ve been wondering how to do this haha. I never did like GM’s audio engine though. I’ve always been put off to DLLs and extensions for ease of use.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How To Run GameMaker On An iPad

Game Review – Poker Squares