Game Development Community

dev|Pro Game Development Curriculum

Triggered Sound

by Dylan Jones · 11/27/2005 (10:44 pm) · 7 comments

Hello everyone! I am not a pro, but just a mere "newbie". This is my first working code with Torque script, so dont except a explanation for each thing, because I dont fully understand it all myself, but I will try my best.

I have looked around this site, and I have found some music/sound tutorials, but none of them have been working for me. The guy who was doing the 4 parts of sound and one of them was Triggered sound, but that was four years ago, and still no tutorial(if their is please put link). Lets begin!

Step One.


First of all since this is not a trigger tutorial you will need to know how to make a trigger, so if your a new one go here; www.codesampler.com/torque.htm then download the Tutorial Base then go down and download the trigger tutorial, then read through that and get the trigger working somewhere in that map or your own.

Step Two.

We need to setup a AudioDescription, and AudioProfile. To do this go to your AudioProfile.cs which is in client/scripts . now at the top of the code we need to make a new channel for our new sound. Think of it like a slot for your new sound to go. um...like a toaster and you want to make 3 pieces of toasted instead of two you add on another slot! To do this put in;

$MusicAudioType = 4;

if you only have two before this one(channels) then put 3 instead of the 4. It should go like this; 1,2,3,4. Im sure you can figure it out!

Step Three.

Now on to those AudioDescriptions, and AudioProfiles! Heres the code for a Profile;

new AudioDescription(NewSound)
{
   volume   = 2.0;
   isLooping = false;
   is3D     = false;
   type     = $MusicAudioType;
};

Now you can see the "type" is the channel, thats clears the whole channel thing up! You can change NewSound to anything you want! Perhaps CampFireNoise or OldGuyYell, whatever suits the situation.

Now for the AudioProfile;

new AudioProfile(NewSoundStart) 
{ 
filename = "~/data/sound/YourSound.wav"; 
description = "NewSound"; 
preload = false; 
};

As you can see the description is whatever you made it above. See the connection? By the way all of this should be going into the bottom of AudioProfiles.cs .

Step four

Getting the bloody sound to start and stop when its supposed to!

if you used the CodeSampler tutorial this should be easy to do, if you havent, then im sure you can just as easily do it, but just make sure you read it carefully so you dont put the stop where it should be a play.

open your trigger.cs location= server/scripts

now your second function should look like this(codeSampler Tutorial.base);

function TempleOfEvilTrigger::onEnterTrigger( %this, %trigger, %obj )
{
      echo( "The player has entered the Temple of Evil! - Calling all monsters!");
      Parent::onEnterTrigger( %this, %trigger, %obj );
}

Ok, I redid the echo mainly because I got rid of it for the final code, but if you read the other tutorial its the function when the player hits the trigger, Player in evil temple dealy. Now make that code this;

function TempleOfEvilTrigger::onEnterTrigger( %this, %trigger, %obj )
{
 $musicHandle = alxPlay(NewSoundStart);
	Parent::onEnterTrigger( %this, %trigger, %obj );
}

the $musicHandle is variable that you are declaring, and its says play this AudioProfile! for a real world example, its like um...a traffic lights red color is called the musicHandle (for pretend ^_^ ) and when its goes green or yellow you hear nothing..silence...but when it goes red you hear the music! then it goes to yellow, and it all stops...that should do the trick! when you hit the trigger in your level the red light will go on and your music/sound will play. Now we need to make so a yellow/green light will make it stop.

In the same file (trigger.cs) the next function under that one above put this(bold part);

function TempleOfEvilTrigger::onLeaveTrigger( %this, %trigger, %obj )
{
[b] alxStop($musicHandle);[/b]
	Parent::onLeaveTrigger( %this, %trigger, %obj );
}

now when you stop touching the trigger the music will stop, thus the yellow light is on, and every one drives slow.

now you should have a trigger that plays sound! just for some shameless selfpromotion download my techno song and use it for this tutorial! Just put into your sound folder and bam!

www.imouse.hostultra.com/YourSound.wav

Thanks for your time! if you got troubles post comment, and I see what I can do, or someone else will fix your worries. In my new site (not done yet) I will be posting all my tutorials that I create learning Torque. This tutorial benefits you and me, because I forget stuff lol. I just about ten minutes ago got this working, so best of luck to you!

Take Care!
Requiem Game designs

#1
11/19/2005 (3:29 am)
By the way that Techno song of mine, is a style that I have never done, its kind of a new wave for me lol I usually do happy hardcore techno! If anyone wants a link to my song entitled "We are the Knights who say NIH!" I will happy to post a link!
#2
11/28/2005 (6:17 am)
it is easier to have it play for just the object in the trigger with %obj.play2d(profileName); I found using alxPlay and alxStop cause lots of issues with other sounds
#3
11/28/2005 (7:39 am)
Correct me if I'm wrong, but as I remember, 1.0 is the maximum volume in an audio description.

Useful resource which can be applied to many different things, (IE, different music below water, different ambient noise within caves, etc.)

Good one.

-Griff
#4
11/28/2005 (2:40 pm)
thanks Griffin!

I am a beginner so, there is probably a much way to do this, but atleast its something, I seen this question in someother resource, and no one answered. lol

Take Care!
#5
11/29/2005 (3:38 pm)
@Dylan, thanks - good stuff

@Anthony, I have also had problems with alxPlay, but I dont quite get your %obj.play2d(profileName); idea, please can you explain a bit more?
This goes in the onEnterTrigger function right? and NewSoundStart is the profileName in this example? is "play2d" defined?
Is there anyway to stop it like in Dylans example? in the onLeaveTrigger perhaps - if so what syntax?

Cheers
Rob
#6
12/18/2005 (6:05 am)
Top job Dylan.
#7
01/12/2006 (12:01 pm)
Nice job, Dylan. I liked the example techno song as well!

I want to point out that Torque supports Ogg Vorbis, so you could take that 34M WAV file and convert it to OGG, likely saving you about 30M. Then just change your profile to:
new AudioProfile(NewSoundStart)
{
   [b]filename = "~/data/sound/YourSound.ogg";[/b]
   description = "NewSound";
   preload = false;
};