Game Development Community

Comparing ObjectTypes

by Jason Hardesty · in Torque X 2D · 11/25/2007 (4:05 pm) · 5 replies

I want to have a health drop when an enemy is killed and am having problems with checking object types.

public static void DamagingCollision(T2DSceneObject ourObject, T2DSceneObject theirObject, T2DCollisionInfo info, ref T2DResolveCollisionDelegate resolve, ref T2DCollisionMaterial physicsMaterial)
{
ShipMovementComponent victimControls = theirObject.Components.FindComponent();

T2DAnimatedSprite explosionSprite = TorqueObjectDatabase.Instance.CloneObject("ExplosionTemplate");
TorqueObjectType enemy = TorqueObjectDatabase.Instance.GetObjectType("enemy");

if (victimControls != null)
{

victimControls.HitPoints--;
if (victimControls.HitPoints <= 0)
{
Game.Instance.Lifes--;
theirObject.MarkForDelete = true;
}

if( theirObject.objectType == enemy)
{
if(.05 < TorqueUtil.GetRandomFloat(1.0f) )
{
T2DSceneObject healthDrop = TorqueObjectDatabase.Instance.CloneObject("Health");
healthDrop.Position = theirObject.Position;

healthDrop.Physics.Velocity = new Vector2(0.0f, -10.0f);

TorqueObjectDatabase.Instance.Register(healthDrop);
}
}
}

Basicly this is like the damageCollision in one of the new tutorials. What I dont understand is which is the enemy if I shoot a projectile or run in to it? The thierObject or the ourObject? Also why cant I check if theirObject.objectType == enemy? They both are an object type but I cant see if they are of the same type? Is there any other way?
Thanks Much!

#1
11/26/2007 (6:39 am)
Just from a pure C# perspective in regards to checking types, have you tried:

if (theirObject.ObjectType is Enemy)
{
  // some code
}
#2
11/26/2007 (9:37 am)
I just tryed that and when I try to type enemy it is not found. I dont understand why u can not use "==" operator they are both TorqueObjectTpyes. This is the error. Anyone have any ideas?

Error
Operator '==' cannot be applied to operands of type 'GarageGames.Torque.Core.TorqueObjectType' and 'GarageGames.Torque.Core.TorqueObjectType'

Thanks David for the idea though.
#3
11/26/2007 (11:19 am)
My guess would be you need to have a reference to where enemy is defined, so you can reference it in code, by that I mean namespace.objecttype. If it still cant find it, I would check the using section and your references list. If it can find it, it should show up colored and show information when you hover over it.

The "==" is usually used for value types( and usually strings too which are reference types), and if used with reference types (such as your objects), should return true if the objects both reference the same object in memory. If you want to see if they are the same object type, you usually use the "is" keyword.

My guess on your error is that you are using "==" on two reference types, which have not implemented how to handle "==" (and "!=" as well). I think if your enemy object is defined then the "is" keyword should work fine.
#4
11/26/2007 (1:01 pm)
@Jason - Try:
if (theirObject.TestObjectType(enemy))
{
    ...
}
#5
11/26/2007 (6:31 pm)
Thank you Dan! That worked great!