Sounds Good To Me

I’ve been doing some class work with audio lately.

It’s all well and good developing a game with all the gameplay in the world and no real graphics or audio to speak of, but eventually it needs both. I’m tackling audio first because my skills in that department are just about better than my skills in the other. And I’m being generous by using the word skills.

Sound effects (SFX) are a great way of spicing up a game, because they provide lovely moments of sonic feedback. It’s amazing how much better the game seems when you hear a little effect if the disc lands in the correct box. In order to implement SFX I had to figure out a way of doing it so that it was usable across all the different game types without me having to type all that boring sound code every time.

So, as usual, I created a class to handle it all for me.

My sfx class initialises all the various sounds that I may require during the game. A set of methods are then provided that allow me trigger an effect at a given volume.

The sfx class looks look this:

local helper = require(“helper”)

— This table is used to contain all Class definitions
local M = {}
— Class
local function sfx(pIsSoundOn)

local mObj = {}

— pIsSoundOn should come from game options
local mIsSoundOn = pIsSoundOn

local mVictory1 = audio.loadSound(“sfx-victory.mp3”)
local mVictory1Channel = 0

——————————————————————————–
— Method: victory1
— Params volume
— Usage: myObj.victory1(0.55)
— Notes: Play sound
——————————————————————————–
local function victory1(volume)

— Only play if Sound is on

if mIsSoundOn == false then

return

end

mVictory1Channel = audio.findFreeChannel()
audio.setVolume(volume, mVictory1Channel)
audio.play(mVictory1, {channel = mVictory1Channel})

end
mObj.victory1 = victory1
——————————————————————————–
——————————————————————————–
— Method: destroyObject
— Params:
— Usage: myObj.destroyObject()
— Notes: Clear an object’s values, e.g. Display Objects, listeners, events etc
— Handy method for ensuring all references are nil etc
——————————————————————————–
local function destroyObject()

— Nil all items in the mObj table
helper.soundDisposal(mVictory1, mVictory1Channel)

mObj = nil — This destroys the object totally, i.e. it can no longer be used

end
mObj.destroyObject = destroyObject
——————————————————————————–

— Return object

return mObj

end

— Assign the class to the Table
M.sfx = sfx
—————————————————————————————————-

— Return the table with all Class definitions as properties

return M

 

The code above only has one sound effect in the class, but this is enough to highlight how it works. In order to use my sfx class in my game I can do the following:

 

local sfx = require(“sfx”)

local mSounds = sfx.sfx(true)

mSounds.victory1(0.75) — Plays the victory sound at a volume of 0.75

 

And that is it. Now every time I create a new Lua file that needs sound, I can use the code above to create my sfx object and play whichever sounds are required.

It’s not perfect, but it does work well and encapsulates all my SFX code into one place that can be reused across many scenes.

Next time I’ll tell you all about storing statistics for my game. So if you like SQL and data don’t miss it!

%d bloggers like this: