One sound, more objects?

DiNeRo

08-11-2012 14:04:17

Hi, im working on a semestral project and im using OgreOggSound.
Ive hit a problem though, lemme explain the situation properly:
There are various instances of the same enemy in the scene and all of them should use the sound "shoot.ogg". I can easily create the sound using sceneManager->createSound("enemyFire","shoot.ogg",....), ive already attached the listener to the camera
void Audio::setEars(Ogre::SceneNode* node){
node->attachObject(soundManager->getListener());
}

and i can also attach it to an object using my own method
void Audio::attachSoundToObject(string name, Ogre::SceneNode *object){
auto sound=sounds.find(name);
if(sound!=sounds.end()){
object->attachObject(sound->second);
} else {
throw Ogre::Exception(3502,"Sound "+name+" is not loaded properly.", __FILE__);
}
}


this is where two questions arise:
1) how can i now access the sound and play it? object->getAttachedObject(name) wont be an OgreOggISound, so i cant use the play() method
2) as i said, there are several "identical" enemies flying around in the scene and each of them needs to have a sound that is attached to it and can be played individualy for that one enemy (of course, several enemies can play a sound at once)


thanks :)

stickymango

20-11-2012 12:18:22

Hi DiNeRo,

1) you can either keep a list of objects and their associated sounds and use that to play each sound, if you know which object is which, OR you could use the following:

static_cast<OgreOggISound*>(object->getAttachedObject(name))->play();
2) Create as many sounds as required and attach each sound to each object then use the above to play indivdually.

HTH

DiNeRo

20-11-2012 14:30:16

hey, thanks a lot, id hoped thered be a simpler way of doing this, wouldnt this clutter the memory with the same sound many times? oh well

anyway, i have no idea where the problem might be i keep getting the "Object already attached to node" error even though i shouldnt be, since the Entity id is unique
this is my code now:
auto e = new Entity(id, type);
if (type > 100){
std::string laser="laser";
laser=laser.append(std::to_string((long double)id));
Audio::loadSound(laser, "efx/laser.wav");
Audio::attachSoundToObject(laser,e->getNode());
Audio::playSound(e->getNode(), laser);
}


and the methods used:
void Audio::attachSoundToObject(string name, Ogre::SceneNode *object){
auto sound=sounds.find(name);
if(sound!=sounds.end()){
object->attachObject(sound->second);
} else {
throw Ogre::Exception(3502,"Sound "+name+" is not loaded properly.", __FILE__);
}
}


void Audio::loadSound(string name, string path,float rollOffFactor,float referenceDistance, bool stream, bool loop, bool preBuffer){
OgreOggSound::OgreOggISound* sound;
if(sound=soundManager->createSound(name,path,stream,loop,preBuffer)){
sounds[name]=sound;
sound->setRolloffFactor(rollOffFactor);
sound->setReferenceDistance(referenceDistance);
} else {
throw Ogre::Exception(3501, "Could not load sound "+path, __FILE__);
}
}

stickymango

20-11-2012 15:16:01

If you need multiple sounds in different locations you have to create multiple sound objects, however, the actual sound data is re-used if you are creating static sounds, so memory consumption should be at a minimum.

I don't see an error with the functions you have shown so you will have to debug to find out where the duplication is occurring.

DiNeRo

20-11-2012 20:08:30

yeah, found it elsewhere, thanks a lot!

anyway, heres an outdated video from the project... probably will make a new video once i get some more sounds up and running!

http://www.youtube.com/watch?v=KYQSKeF5KwM

stickymango

03-12-2012 22:55:09

Great start!

Keep me informed how you get on and thanks for looking at OgreOggSound.

DiNeRo

04-12-2012 01:54:06

well, prebeta gameplay here: (also, for some reason, when i uploaded it to youtube, it got much darker than it was before) http://youtu.be/6-j-dV0ZFKI