As the title implies, today we’re going to load a sound resource into GameMaker Studio 2 and then play it from script.

First we need to find a sound.  It doesn’t need to be any particular sound; a game related sound would be nice.  So we navigate to and browse around.  The sound I selected was mouse click.wav.  Download the sound (you’ll have to become a member if you’re not already but it’s a great site for kicking the tires on an idea).

Next, open GameMaker Studio 2 and either create a new project or select an existing one.  In the resources toolbar right click on Sound and choose Create from the menu which pops up.

In the window which appears in the Workspace click the ellipsis (…) and maneuver to the sound we want to have played.

Once you’ve selected the sound name it.  In this window I’ve renamed the sound to snd_mouse_click.

The rest of the items on the window are dependent on your choices and the sound format.  The speaker icon can be used to mute the sound.  The play button to the right of the loop counter is used to preview the sound.  The continuous loop icon after the play button is used to set the sound on continuous loop.  The rewind icon is used to rewind the sound to the beginning.  The button with three people on it is used to open the sound in the sound group mixer, a topic we’ll cover later.

Compressed audio (“Compressed – Not Streamed” and “Compressed – Streamed”) will cause your output to be compressed into Ogg Vorbis sound files, regardless of source.  If you’ve loaded an MP3 file into the editor do not set it as Compressed – you will end up with severe sound degradation in your game.  There’s also overhead to uncompressing any sound.

The slider next to the mute icon is the absolute volume for the sound.

Target options cover how the sound resource is converted for packaging with your game.  You can choose mono, stereo, or 3D for the output target — choose only the level you require, lest you waste memory and CPU.  The sample rate of 44100, quality of 16 bit, and bit rate (kbps) of 128 are good enough for most sounds.  You are free to choose differently based on your needs.

The audio group is used for programmatic manipulation of sound assets, generally to optimize loading or playing of sounds.

This is it for sound setup.  If you don’t know what something in particular is you’re best bet is to leave it alone.  GMS 2 has pretty sane defaults for most of this configuration.

Now, create a sprite, an object, and a room if you don’t already have one.  (See previous articles.)  On the object choose Add Event, Mouse, Left Down.

In the code window enter the following code:

The function is audio_play_sound.  The first argument is the index to the sound — this is just the name you provided the sound on the sound editor window.  The second argument is relative to your game — it specifies the importance of the sound in the grand hierarchy of things.  This setting allows GameMaker Studio to drop lower priority sounds when the maximum number of sounds has been reached.  The third argument specifies whether the sound should loop or not.

Hit F5 to compile and run the project.  Click on your object and you should hear the sound play.  I’m not too embarrassed to say I haven’t figured out how to create room events yet.  I’m learning as I type up these tutorials.