Game Development Community

Static object with sound

by Doddy · in Torque Game Engine · 03/06/2006 (1:52 am) · 5 replies

Please dont flame me if this topic has been discussed a hundred times already. I have searched but cant quite find the info I am looking for.

Basically I am a first timer at using TGE and I started to build from the tutorial.base using codesamplers tuts. So far so good until I decided to do go it alone.

I have had a few good moments until I decided to try and have a go at adding sound to the campfire.

Step 1:
Found that I could use a simple AudioEmitter placed at the same point in Mission Editor. This worked but I was sure there would be a better approach. I may want to add more fires without worring about placing the emitters with them.

Step2:
I hunted around for something that used AudioProfile, the only one I found relevant is ShapeBaseImageData (probably more but my head was hurting when looking through the code). This has an AudioProfile *sound declaration, and seemed fairly relevant to what I was trying to accomplish (by the name) but I havent figure out how to use this yet.

Step3:
Started to try and use ItemData. The shape now appears in mission editor. I was then hoping that I would have a soundProfile option as part of the properties but I didnt :-(.
Also not sure if a bug but the rotate option doesnt appear to have any effect (rotating campfires arent good). I copied this from the superbomb tut. In there there is an Item::create (I thought this was just a namespace override) so I changed it to superbomb::create and also a new one or my campfire with campfire::create (without the rotate option set) but it seems torque script requires Item::create to create superbomb or campfire so there is no way to seperate the two create functions,can you tell me why?

I would appreciate it if there is anyone willing to help out or give links that I have not managed to find. When I fully understand it I think I will create a tut (if there isnt already one) that covers these basics as I think it is one steeping stone required in becoming a good TGE user but doesnt appear to be available yet.

Cheers,

Doddy

About the author

Recent Threads


#1
03/06/2006 (4:49 am)
Hey Doddy

1) Use an audio emitter, you may not like having to repeat this step each time you add a new object but that's the way it's done. If this really bother's you, you could create a script that automatically creates an emitter with your static shape.

2) Static shapes don't have an audio profile, refer point 1.

3) I might be reading you wrong on this, but I gather you are saying that you are unable to stop an item from rotating. If this is the case, flag the item rotate field in the mission editor accordingly, you'll have to restart the mission for the change to take place.

Once again, you could create your own onAdd function for items you don't want to rotate. Something like this will work...

//Your item datablock
datablock ItemData(MyItem)
{
   category = "MyItem";
   className = "MyItem";
   shapefile = "~/data/shapes/bla/bla.dts";
   mass = 55;
   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 3;
   pickUpName = "my item's name";
   computeCRC = true;
   isInvincible = true;
};

// Specify values on adding my item via mission editor
function MyItem::onAdd(%this,%obj)
{
   %obj.rotate=(0); //no rotation on add
}

Hope that helps
#2
03/06/2006 (5:01 am)
Tim,

Thanks for the response.

I am glad in a way that it is not possible....at least it means I am starting to understand how this things works, if only a little. But I do find it strange not being able to do this.

Thanks for the advice on rotation issue. I thought that I had tried this...maybe I just forgot to save restart, it was getting late.

Cheers,

Doddy
#3
03/06/2006 (8:03 am)
Tim,

I have just tried your suggestion for the rotation problem that I have. Here is my code:

datablock ItemData(MyFire)
{
	category = "MyFire";
	className = "MyFire";
	shapeFile = "~/data/shapes/campfire/campfire.dts";
};

function MyFire::onAdd(%this, %obj)
{
	%obj.rotate = (0);
}

This appears to have no effect when I add a MyFire using the mission editor. I insert a breakpoint on the %obj.rotate and the program never gets there.

Is it somehow because the superbomb.cs has an override for ItemData create:

function ItemData::create( %data )
{
	echo( "ItemData::create for SuperBomb called --------------------------" );

	%obj = new Item()
	{
		dataBlock = %data;
		rotate = true; // All Super Bomb power-ups will rotate.
		static = true; // Super Bombs should stay put so they don't slide away.
	};

	return %obj;
}

This was taken from the codesampler tuts but it appears to override all ItemData created which isnt great. Is there something I am missing? But if the onAdd functions wre called this would fix the problem ....but they dont :-(

Cheers,

Doddy
#4
03/06/2006 (8:23 am)
Doddy,

The function in superbomb.cs is just specifying the default value for what to do with items that have no onAdd function specified. It isn't causing you any problems & you need that function so don't delete it. To get your fire item to work do everything the same but remove the reference to classname.

IE...
datablock ItemData(MyFire)
{	
   category = "MyFire";	
   shapeFile = "~/data/shapes/campfire/campfire.dts";
};

Your fire will no longer rotate upon dropping it into the world.

[edit]

To explain a little further, the classname can't share the same name as the item datablock name. The classname is used to specify the same function to multiple items with a common classname. I'm not very good at explaing things so I'll give you an example.

Say you wanted to create 3 fire items, all the same but with different *.dts files (or any other differences such as mass, elasticity etc). You want to create a set of functions for all 3 fire items, instead of creating 3 functions (1 for each item) you could create 1 function that they all share.

datablock ItemData(MyFire1)
{
   category = "MyFire";
   className = "MyFire";
   shapefile = "~/data/shapes/flag/flag2.dts";
};

datablock ItemData(MyFire2)
{
   category = "MyFire";
   className = "MyFire";
   shapefile = "~/data/shapes/flag/flag2.dts";
};

datablock ItemData(MyFire3)
{
   category = "MyFire";
   className = "MyFire";
   shapefile = "~/data/shapes/flag/flag2.dts";
};

function MyFire::onAdd(%this, %obj)
{
   %obj.rotate = (0);
}

In the above example those 3 instances of fire item will all share the same onAdd function. If you poke around weapon scripts like crossbow.cs you'll find they have a classname of 'weapon'. The functions for any images with a classname of weapon are all layed out in weapon.cs. Hope this is easy to understand & my explantion wasn't too crappy.
#5
03/06/2006 (9:22 am)
That did the trick. renamed className to MyFireClass rather than deleting just in case.

Thanks for all the help.

Back on the subject of the AudioEmitters for static object. I am not going to yet, but just making sure I have the correct understanding. To have a static object use an AudioEmitter I would need to:

In the engine
- Create a class (datablock) based on ItemData (or possibly ShapeBaseData). MyFireData
- Add an AudioProfilePtr* to the class.
- Create a class based on Item (MyFire).
- Do the necessary coding for playing the sound once in the game (MyFire::onAdd function in the engine?)

In the console
- MyFire::create(). Similar code as the Item::create in the above message.

Is this pretty much it?

Or an easy way if I didnt want to add them through the mission editor. MyFire is the same class as above with the soundProfile exposed to the console.

function addFire(%position)
{
%obj = new MyFire()
{
soundProfile = CampFireProfile;
position = %position;
}
}


Cheers,

doddy