GameMaker Studio 2: Loading a Sound as an Asset and Playing It

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 www.freesound.org 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.

 


Installing Audacity and the LAME MP3 Encoder

This will get you up and running with the Audacity sound editor, a very capable and historied GPLed sound editor.  Begin by downloading and installing Audacity.  The installer for Audacity itself is pretty clean and informative.  If you can, leave everything at the default — this will ensure everything works when we’re done.

Follow that up by downloading the current binary version of LAME. Some notes: because of intellectual property rights run amok you can’t download a copy of binary LAME from their website. The installer can only be downloaded from websites hosted in countries that don’t have the same level of protectionism. When you go to download Audacity, you’ll actually be directed to http://lame.buanzo.org/#lamewindl. This is fine. If you’re nervous about downloading from this particular website do the research and find another that you’re more comfortable with.

On the #lamewindl page hosted on the buanzo.org website you’ll see a header entitled TO DOWNLOAD Lame and FFmpeg for Windows, click links BELOW:  The installer you want is linked below this header and labeled as the RECOMMENDED Installer Package for Windows.  Run the installer, leave everything at the default value.

If you installed everything into their default locations you should be able to run Audacity and find everything in place to import and export MP3 files, as well as other sound formats.

A note on MP3s:  MP3s are compressed sound.  That means that bits of information have been left off the MP3 file in order to conserve space.  Normally a person won’t object to the difference between an uncompressed music file and an MP3.  If you compress an MP3 again, however, the noise will be very noticeable.  It’s best to always input from uncompressed audio and output to the needed format.

That’s it.  To be honest I thought this would be more work but we’re done.


GameMaker Studio 1.4 in (re)Humble Bundle; Tutorial on 2D Art

GameMaker Studio 1.4 and many of the plugins for it are currently for sale in the (re)Humble Bundle.  $15 gets it all, if you go for the lowest price point.  After you purchase 1.4 via this method you’re eligible to pick up the 2.0 version at a discounted price ($60 instead of $100 for the primary studio).

A solid tutorial on how to develop your own 2D game art can be found on gamedev.net:  https://www.gamedev.net/articles/visual-arts/the-total-beginner%E2%80%99s-guide-to-better-2d-game-art-r2959/.