|
Starting on the Right Note
While it is unfortunate that the TGB editor does not come with built in audio datablock creation and editing, there are some nice workarounds made by the community. This tutorial will cover adding music and sound effects the old fashioned way...through script.
It will also breakaway from the mold of doing everything through a behavior. We'll start by creating a script file. Copy the following script and add it to the gameScripts folder in your project.
audio.cs
//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// Script by Mike Lilligreen for TDN Asteroids Tutorial
//---------------------------------------------------------------------------------------------
new AudioDescription(AudioNonLooping)
{
volume = 1.0;
isLooping = false;
};
new AudioDescription(AudioLooping)
{
volume = 0.8;
isLooping = true;
isStreaming = true;
};
new AudioProfile(spawnAudio)
{
filename = "~/data/audio/Spawn.wav";
description = "AudioNonLooping";
preload = true;
};
new AudioProfile(explodeAudio)
{
filename = "~/data/audio/Explosion.wav";
description = "AudioNonLooping";
preload = true;
};
new AudioProfile(fireAudio)
{
filename = "~/data/audio/Fire.wav";
description = "AudioNonLooping";
preload = true;
};
new AudioProfile(backgroundMusic)
{
filename = "~/data/audio/MainTheme.ogg";
description = "AudioLooping";
preload = false;
};
The looping and non-looping descriptions are set up, plus audio profiles for each of the 3 sound effects and background music. Due to the small file size of the sound effects, we can load them at game startup without adversely affecting the overall game loading time. The OGG file has preload set to false to allow it to load after the game has started, where we can hide the loading time a bit better - while the player is on the title screen for example. Since you don't have a title screen at the moment and all level testing is being done through the TGB editor, preload can be set to true if you wish.
Wondering where these audio files are though? Download them here or if you have already downloaded the Astral Objects game, you can copy the files from the data/audio folder to your project's data/audio folder.
Note: MainTheme.ogg aka Hiscore Theme from Catgun is distributed through the following creative commons license. All sound effects from the folks at GarageGames (T2D 1.0.2).
With audio.cs now saved, let's make sure the engine can find it. In your project's game folder, open up main.cs and change initializeProject with an additional exec for the new audio script file:
function initializeProject()
{
// Load up the in game gui.
exec("~/gui/mainScreen.gui");
// Exec game scripts.
exec("./gameScripts/game.cs");
exec("./gameScripts/audio.cs");
// This is where the game starts. Right now, we are just starting the first level. You will
// want to expand this to load up a splash screen followed by a main menu depending on the
// specific needs of your game. Most likely, a menu button will start the actual game, which
// is where startGame should be called from.
startGame( expandFilename($Game::DefaultScene) );
}
Save the file and let's continue on with some more script editing.
Up the Tempo
Our sound effects and music are ready to be played, we just need to find the appropriate spot to call them. Let's start with the 3 sound effects. The spawn sound effect will be played when the player respawns at game start or after a death. So why not "attach" it so to speak along with the particle effect that gets displayed? In takesDamage.cs change the spawn function to the following:
function TakesDamageBehavior::spawn(%this)
{
%this.owner.collisionActiveReceive = true;
%this.schedule(%this.invincibleTime * 1000, "setVincible");
%this.health = %this.startHealth;
%this.owner.setBlendColor(1, 1, 1, 1);
%this.owner.visible = true;
%this.owner.control = 1;
%this.owner.setFrame(%this.startFrame);
if (%this.isPlayer == true)
%sound = alxPlay(spawnAudio);
if (isObject(%this.respawnEffect))
{
%explosion = %this.respawnEffect.cloneWithBehaviors();
%explosion.position = %this.owner.position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
}
}
Here we check to make sure it is the player that is spawing and not an asteroid. Then we use alxPlay on spawnAudio. Simple, yes? While we are in the script, let's set up the explosion sound effect too. Right below the spawn function is the explode function, change it to:
function TakesDamageBehavior::explode(%this)
{
if (isObject(%this.explodeEffect))
{
%explosion = %this.explodeEffect.cloneWithBehaviors();
%explosion.position = %this.owner.position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
if (%this.isPlayer == true)
%sound = alxPlay(explodeAudio);
}
}
If you want the asteroids to have this sound effect too, you can get rid of the if conditional.
Save the behavior. There's still one more sound effect (fireAudio). In Astral Objects, it was used only for the UFO firing projectiles which is not covered in this tutorial series. If you wish to add this to the player ship firing, the shoots behavior would be a good place for it.
We still have the background music to add though. It would be nice to start it when the scene is loaded, so let's find a good place for it. We already have a singular scene object that controls all asteroid spawning called spawnMachine (or whatever you named the object that has the spawn area behavior). Add the following line to the onAddToScene function in spawnArea.cs.
$backgroundMusic = alxPlay(backgroundMusic);
The music has been saved to a global variable $backgroundMusic to give us the flexibility to turn it off when needed. With the sound effects we don't have this issue since they are only played once without looping.
While this section of the tutorial does not have a specific spot where the music needs to stop, it can be ended in script or through the console with
alxStop($backgroundMusic);
Remember to save all your script and behavior files and try out the changes in TGB. Hear the difference? The next section continues with another important aspect of a game - the Title Screen.
Return to the Asteroids Tutorial Hub
|