Game Development Community

Triggered Sound

by Ben Ewing · in Torque Game Engine · 12/12/2005 (8:29 pm) · 13 replies

Ok Im trying to get a song to play once the player has entered an area, I have the trigger, audio description/profile set up, but it wont work

Triger.CS:
"datablock TriggerData(ShoppingSounds)
{
tickPeriodMS = 100;
};

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

function ShoppingSoundsTrigger::onLeaveTrigger(%this,%trigger,%obj)
{
$musicHandle = alxStop($musicHandle);
Parent::onLeaveTrigger(%this,%trigger,%obj);
}

function ShoppingSounds::onTickTrigger(%this,%trigger)
{
Parent::onTickTrigger(%this,%trigger);
}"

Client/Audioprofiles.cs
"new AudioDescription(Shopping)
{
volume = 2.0;
isLooping = false;
is3D = false;
type = $MusicAudioType;
};

new AudioProfile(Shopping)
{
filename = "starter.rpg/data/sound/Street.wav";
description = "Shopping";
preload = false;
};

"

Server/AudioProfiles.cs
"new AudioDescription(Shopping)
{
volume = 2.0;
isLooping = false;
is3D = false;
type = $MusicAudioType;
};

new AudioProfile(Shopping)
{
filename = "Starter.rpg/data/sound/Street.wav";
description = "Shopping";
preload = false;
};
"

flat.mis
" new Trigger(ShoppingTime) {
position = "126.468 315.323 46.7";
rotation = "1 0 0 0";
scale = "106.014 294.04 108.362";
dataBlock = "ShoppingSounds";
polyhedron = "0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 -1.0000000 0.0000000 0.0000000 0.0000000 1.0000000";
};"

#1
12/12/2005 (8:36 pm)
Skip the new when you declare audioprofiles and descriptions server side.
#2
12/12/2005 (8:54 pm)
I'm with billy, only declare it once and you'll be fine. Also a volume of '2' is no different than a volume of '1'. '1' is the max allowable volume in the engine anyhow (unless you made some code changes not mentioned here).

I would do this...in server/scripts/audioprofiles.cs place this:

datablock AudioDescription(Shop)
{
   volume   = 1.0;
   isLooping= false;

   is3D     = false;
   type     = $musicAudioType;
};

datablock AudioProfile(Shopping)
{
filename = "~/data/sound/street.wav";
description = Shop;
preload = true;
};

I'm not sure if the AudioDescription & AudioProfile can share the same name so i named one 'Shopping' & the other 'Shop'. Also delete whatever you have in client/scripts/audioprofiles.cs and you should be fine.
#3
12/12/2005 (9:16 pm)
Signifigance of this?:
preload = true;
#4
12/12/2005 (9:30 pm)
It's not necessary but what it does is preload the sound into memory as the mission is loading and stores it for later use, as opposed to having to load the file 'on the fly'. Ultimately what this means is that your mission will take a little longer to load but you will benefit from having less demand on resources in game. I'm happy to increase loading time of missions if it speeds up framerate in game.
#5
12/12/2005 (9:49 pm)
So, the contents of client/audioiprofiles, isnt needed?
And I still cant get it to work...:(
#6
12/12/2005 (10:11 pm)
-In your trigger script (and audioprofile script) you seem to have a few " which don't need to be there. Get rid of them.

-Also get rid of the Parent::onLeaveTrigger(%this,%trigger,%obj); stuff in all places.

-Check the console to check for script warnings / errors.

-Type $musicHandle = alxPlay(Shopping); in the console to see if that's working.

-Put this in your function ShoppingSounds::onEnterTrigger(%this,%trigger,%obj)
section to make sure the trigger is working... MessageAll("","Entered Trigger"); It will display that message in the main chat hud when you enter trigger.

-No, you don't need anything in client/audioprofiles.

-Try using an ogg instead of wav

-I put this in and it worked for me so either you have a typo somewhere or it doesn't like your sound file.
#7
12/12/2005 (10:38 pm)
Here's my exact scripts if it helps, obviously the only difference will be the path to the wav file:

Triger.cs
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// DefaultTrigger is used by the mission editor.  This is also an example
// of trigger methods and callbacks.

datablock TriggerData(ShoppingSounds)
{
   // The period is value is used to control how often the console
   // onTriggerTick callback is called while there are any objects
   // in the trigger.  The default value is 100 MS.
   tickPeriodMS = 100;
};


//-----------------------------------------------------------------------------

function ShoppingSounds::onEnterTrigger(%this,%trigger,%obj)
{
   // This method is called whenever an object enters the %trigger
   // area, the object is passed as %obj.  The default onEnterTrigger
   // method (in the C++ code) invokes the ::onTrigger(%trigger,1) method on
   // every object (whatever it's type) in the same group as the trigger.

   $musicHandle = alxPlay(ShoppingWav);
   echo("Trigger Entered");
}

function ShoppingSounds::onLeaveTrigger(%this,%trigger,%obj)
{
   // This method is called whenever an object leaves the %trigger
   // area, the object is passed as %obj.  The default onLeaveTrigger
   // method (in the C++ code) invokes the ::onTrigger(%trigger,0) method on
   // every object (whatever it's type) in the same group as the trigger.

   $musicHandle = alxStop(ShoppingWav);
   echo("Trigger Exited");

}

function ShoppingSounds::onTickTrigger(%this,%trigger)
{
   // This method is called every tickPerioMS, as long as any
   // objects intersect the trigger. The default onTriggerTick
   // method (in the C++ code) invokes the ::onTriggerTick(%trigger) method on
   // every object (whatever it's type) in the same group as the trigger.

   // You can iterate through the objects in the list by using these
   // methods:
   //    %this.getNumObjects();
   //    %this.getObject(n);

}

and in server/scripts/audioprofiles.cs

datablock AudioDescription(Shopping)
{
   volume   = 1.0;
   isLooping= false;

   is3D     = false;
   //ReferenceDistance= 100.0;
   //MaxDistance= 1000.0;
   type     = $MusicAudioType;
};

datablock AudioProfile(ShoppingWav)
{
filename = "~/data/shapes/m60/m60_sound_reload.ogg";
description = shopping;
preload = true;
};
#8
12/12/2005 (10:42 pm)
N.B.

The $musicHandle = alxPlay(ShoppingWav); should lead to the name of the AudioProfile & not the AudioDescription. Also The AudioProfile & AudioDescription cannot be named the same.

If you paste in my scripts posted above (only changing the *.wav path)it should work.
#9
12/12/2005 (11:37 pm)
Just found an error regarding getting the sound to stop.

Make sure you have this in client/scripts/audioProfiles.cs

// Channel assignments (channel 0 is unused in-game).

$GuiAudioType     = 1;
$SimAudioType     = 2;
$MessageAudioType = 3;
[b]$MusicAudioType   = 4;[/b]

Finally the syntax $musicHandle = alxStop(ShoppingWav); in Triger.cs is wrong. Instead use...

alxStop($musicHandle);

Ok, that's it. If you follow my instructions correcly it will all work fine.
#10
12/13/2005 (7:01 am)
Thankyou!!!! :)
#11
12/13/2005 (7:04 am)
Glad to help
#12
12/16/2005 (2:38 am)
Big thanks Tim! I finally got my music to stop whenever I need to!
#13
12/16/2005 (3:42 am)
Always happy to help, though i feel most credit should go to Dylan Jones for submitting his resource on triggered sounds. All the information is in that resource, all i did was restate what was already outlined in that resource. Anyhow, glad i was of some assistance and pleased to hear you finally got your music to stop :)