May 22, 2012, 04:55:51 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Please report any bugs or issues that you might be encountering with the Beta in the Support System so that we can better keep track of any oustanding issues that may come up.

GameCore Support System
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Adding Sound Effects to an Object  (Read 656 times)
Delerna
Jr. Member
**
Posts: 50


View Profile
« on: August 29, 2008, 05:46:22 PM »

I have an spear object and I am toggling its position vertically at regular intervals so that it sticks up through the floor and then retracts.
Now I want to add some sound effects so that it makes a clunk sound when it comes up and a psssst sound when it goes down.

Went to the documentation and there is no help whatsoever on sound.
Can someone help me with some example code to put into the objects controller script.
To play a sound effect. I guess the sound should be loaded somewhere else other than the controller script.
Some help with that would be appreciated also.
Thanks in advance for any help provided
Logged
gekido
Guest
« Reply #1 on: August 29, 2008, 06:14:13 PM »

Is this done via an animation or are you controlling the object from script?  Could be done with either.

We've added a new function for simply triggering one-shot sounds, called PlaySample():

http://www2.gamecore.ca/docs/scripting-reference/classes/FXManager/PlaySample

Code:
GetFXManager().PlaySample("mySound.smp");

This can be used for simple one-shot sounds.  The other option is to 'wrap' the sound in an effect (using the FX Editor) - any particle can have a sound attached in the 'Sample' field.
Logged
Delerna
Jr. Member
**
Posts: 50


View Profile
« Reply #2 on: August 29, 2008, 06:29:33 PM »

I am doing this in script. All my objects (except the player) are simple "box without collision" at the moment.
I am experimenting with scripted movement of objects. Rotation, re-positioning etc.
There will be a tutorial of this added to the
"Create Tutorials For Inclusion In The Game Core Tutorials Menu"
http://www2.gamecore.ca/forums/index.php?topic=34.0
It will be a simple "run the gauntlet" game. Basically a corridor with swinging blades and stabbing spears and a player to navigate safely through it.
All working now, just got to work on the collisions.(thats going to challenge me I think, I have no idea at the moment)



Actually, not long after I posted the question I thought, "Why not check the BV documentation" and there it all was.

So here is what I did (for anyone else who may read this with a similar problem)

In the gsl file for the scripted object

1) At the top of the file create a global variable of type sample. (Global for all fuctions within this particular script file, that is)
     Sample MySound;

2) In the Initialise function load and setup the sound
    Pos=object.GetPosition(); //Get the position of the object so the sound can be set to the same place
    MySound.Load("woosh.wav"); //I have the sound file in the same location as the object and gsl files
    MySound.SetVolume(1);
    MySound.SetPosition(Pos);
    MySound.SetMinDistance(1);

3) Now in the function where movement occurs play the sound. I have it in the DynamicsAdvance function
    MySound.Play();


The only problem I have now is that I have 3 objects all using this 1 script.
This must mean I am loading the same sound file 3 times.

Where should I move the loading of the sound so that it is loaded only once?

   
« Last Edit: August 29, 2008, 06:47:31 PM by Delerna » Logged
gekido
Guest
« Reply #3 on: August 29, 2008, 06:32:11 PM »

Internally the sound will be only loaded once and reused, so you should be safe doing it how you are.  Every object / script is a seperate 'instance' so you can't really have a shared pool of resources (from script) to handle things - but the engine does manage this internally automatically for you.

How you are doing it (manually creating the Sample object and reusing it) is more how you would want to do this for sounds that are more 'persistent', like an engine idle sound for example - this way you can modify / manipulate the sound as it's played from script (by setting the frequency for example to 'rev' the engine).

The way that I mentioned (via PlaySample) is more for one-shot 'effects' type sounds like a bullet ricochet or something similar that is loaded, played and discarded.
« Last Edit: August 29, 2008, 06:34:27 PM by gekido » Logged
Delerna
Jr. Member
**
Posts: 50


View Profile
« Reply #4 on: August 29, 2008, 06:34:14 PM »

Oh.....OK. Thats good to know....and well done on that!
Actually, I remember reading that somewhere before, now that you mention it

How am I doing it?
What I am doing is In the DynamicsAdvance function
I increment a variable by the seconds parameter passed to the function multiplied by a random number less than 1.
the randomness stops the 3 spears all moving at the same time all the time.
Then I check the variable and if its greater than 1 then I move the spear and play the sound

like this

void DynamicsAdvance( Object@ object, float seconds)
{   cnt=cnt+(seconds*RandomFloat());
   if(cnt>=1)
   {   Pos=object.GetPosition();
      Pos.y=Pos.y*-1; //This works because the top surface of the floor is at ypos=0.
                   //This means that multiplying the objects ypos by -1 toggles the object between
                      //two positions. Above the floor and below the floor
      object.SetVelocity(0,0.05,0);
      object.SetPosition( Pos);
      cnt=0;
      MySound.Play();
   }
}

So yea, I manually create the sound and play it over and over.
Oh and SetVelocity was my attempt at slowing the repositioning down so it moved a bit more smoothly.
It seems the velocity has no effect on SetPosition. I am trying to work out which method I need to use
with SetVelocity to work. I don't want to slow it down much just enough so it is obvious that the spears are
comming up through the floor instead of APPEAR....DISAPPEAR..APPEAR..DISAPPEAR  etc..etc
« Last Edit: August 29, 2008, 07:05:54 PM by Delerna » Logged
gekido
Guest
« Reply #5 on: August 30, 2008, 12:34:50 PM »

what you'd probably want to do is something similar to how the door script works:

some variables:
Code:
float maxRotation = 1.57;  // open 180 degrees (in radians)
float rotAmount = 0; // how much the door is rotated at the moment
float openRemain = 0; // how long the door has been open
float openTime = 4; // how long should the door stay open

the important part of the door, ie the opening / closing action:
Code:
// is this door unlocked / active?  ie can it open
if (active)
{
// is the door opening?
if( openRemain > 0)
{
rotAmount -= 0.05;

if (rotAmount < -maxRotation)
rotAmount = -maxRotation;
}
else
{
// close door
openRemain = 0;

rotAmount += 0.25;
// are we closed?
if (rotAmount > 0)
rotAmount = 0;
}
}

ie, instead of simply moving the spear the entire distance that you want to in a single call, move it partially every iteration until it's moved 'enough' and then reverse the operation.

I've stripped out the bulk of the script to keep it simple, but basically this checks to see if the door is active (ie should be moving) and then does the rotation either opening or closing.

the openRemain variable is set once the door is fully open to check how long it should stay open before it closes again.

Here's the full script:

Code:

//------------------------------------------------------------
//
// Example script for  rotating doors
//
//------------------------------------------------------------

bool debug = false;

// constants
float maxRotation = 1.57;  // open 180 degrees (in radians)
String userTip;

// variables
// how often to display the 'interact with object' message
float respawnTime = 3;
// time we have shown the 'interact with object' message
float timeToRespawn = 0;
bool interact = false;

// variables
bool hingeSide = false;
bool side = false;
float openRemain = 0; // how long the door has been open
float openTime = 4; // how long should the door stay open
float rotAmount = 0; // how much the door is rotated at the moment
bool active = false; // is this door activated?
bool oneShot = false; // is this a one-shot door?
Matrix startingMatrix;

//------------------------------------------------------------
//
// void Initialize( Object@ object)
//
// This function is called when the controller is created.
// Any initialization should be done here.
//
// Parameters:
//
// object - The object that this controller is to act upon.
//

void Initialize( Object@ object)
{
    SetAngularMode(MODE_RADIANS);
   
startingMatrix = object.GetCurMatrix();
hingeSide = object.GetControllerSettingBool( "HingeSide");
userTip = object.GetControllerSettingString( "UserTip");
active = object.GetControllerSettingBool( "Active", true);
oneShot = object.GetControllerSettingBool( "OneShot", false);
}


//------------------------------------------------------------
//
// bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
//
// All of the keys that can be pressed for this object are
// passed to this function every frame.
//
// Parameters:
//
// object - The object that this controller is to act upon.
//
// event - Name of the event type
//
// params - Parameters for the event (specific to each event type)
//

bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
if (params.player != null)
{
if (debug)
Print("Door in Player Inventory, Handling Events");
// are we opening
if (event == "Input" && params.stringValue == "UseObject")
{
if (params.floatValue >= 0.5f && openRemain <= 0)
{
openRemain = openTime;
Vector v = params.player.GetInventoryObject( "Player").GetPosition();

// which side is our hinge on?
if( hingeSide)
{
if ( object.GetCurMatrix().ProjectInverse( v).z > 0)
side = true;
else
side = false;
}
else
{
if ( object.GetCurMatrix().ProjectInverse( v).z > 0)
side = false;
else
side = true;
}
}
return true;
}

}
return false;
}

//------------------------------------------------------------
//
// void DynamicsAdvance( Object@ object, float seconds)
//
// This function is called once per dynamics step.  Any
// object movement should be done here.
//
// Parameters:
//
// object - The object that this controller is to act upon.
//
// seconds - How much time has passed since last call.
//

void DynamicsAdvance( Object@ object, float seconds)
{
// is this door unlocked / active?  ie can it open
if (active)
{
// is the door opening?
if( openRemain > 0)
{
if (side)
{
rotAmount += 0.05;

if (rotAmount >= maxRotation)
rotAmount = maxRotation;
}
else
{
rotAmount -= 0.05;

if (rotAmount < -maxRotation)
rotAmount = -maxRotation;
}
}
else
{
if (!oneShot)
{
// close door
openRemain = 0;

if (side)
{
rotAmount -= 0.25;

// are we closed?
if (rotAmount < 0)
rotAmount = 0;
}
else
{
rotAmount += 0.25;
// are we closed?
if (rotAmount > 0)
rotAmount = 0;
}
}
}
}

object.GetCurMatrix() = startingMatrix;
object.GetCurMatrix().Rotate( rotAmount, 0, 0);
object.UpdateCollisionObjectPositions();
openRemain -= seconds;

// see if it's time to hide our tip
if (timeToRespawn > 0)
{
timeToRespawn -= seconds;
if (timeToRespawn <= 0)
{
timeToRespawn = 0;
HideForm("TipForm");
}
}
}


//------------------------------------------------------------
//  reset the interact variable
void SetControllerVariable( Object@, const String& in name, bool value)
{
    if (name =="interact")
{
Print("Player interacting with Door");
        interact = value;
}
if (name =="active")
{
Print("door activated / unlocked");
        active = value;
}
if (name == "PickedUp")
{
// setup our tip text
print("show tip");
GetControl( "Message", "TipForm").SetText( userTip);
ShowForm("TipForm");

timeToRespawn = respawnTime;
}
    return;
}

bool GetControllerVariableBool( Object@ object, const String& in name)
{
if (name == "interact")
        return interact;
if (name == "active")
        return active;
if (name == "CanBePickedUp")
return active;
return false;
}

//------------------------------------------------------------
Logged
Pages: [1]
  Print  
 
Jump to:  

 
Powered by MySQL Powered by PHP bluBlur Skin © 2006, hbSkins
Powered by SMF 1.1.14 | SMF © 2006-2011, Simple Machines LLC
Valid XHTML 1.0! Valid CSS!