Game Development Community

Safe Delete(); not doing its job?

by Steven Sheffey · in TGB Platformer Kit · 09/12/2008 (6:36 pm) · 3 replies

Hey guys this might get a lil lengthy but my problem lies in the DealsDamageBehavior. Here is my code.

//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

if (!isObject(DealsDamageBehavior))
{
%template = new BehaviorTemplate(DealsDamageBehavior);

%template.friendlyName = "Deals Damage";
%template.behaviorType = "Damage";
%template.description = "Set the object to deal damage to TakesDamage objects it collides with";

%template.addBehaviorField(strength, "The amount of damage the object deals", int, 100);
%template.addBehaviorField(deleteOnHit, "Delete the object when it collides", bool, 1);
%template.addBehaviorField(explodeEffect, "The particle effect to play on collision", object, "smoke", t2dParticleEffect);
}

function DealsDamageBehavior::onBehaviorAdd(%this)
{
%this.owner.collisionCallback = true;
%this.owner.collisionActiveSend = true;
}

function DealsDamageBehavior::dealDamage(%this, %amount, %victim)
{
%takesDamage = %victim.getBehavior("TakesDamageBehavior");
if (!isObject(%takesDamage))
return;

%takesDamage.takeDamage(%amount, %this);
}

function DealsDamageBehavior::explode(%this, %position)
{
if (isObject(%this.explodeEffect))
{
%explosion = %this.explodeEffect.cloneWithBehaviors();
%explosion.position = %position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
}
}

function DealsDamageBehavior::onCollision(%this, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
%this.dealDamage(%this.strength, %dstObj);

if (%this.deleteOnHit)
{
%this = %this.cloneWithBehaviors();
%this.explode(getWords(%contacts, 0, 1));
echo("Start safe Delete");
%this.owner.safeDelete();
echo("End safe Delete");
}
}


For some reason safeDelete() will not delete the projectile when it collides with anything, whether that be the ground or the enemy using the TakesDamageBehavior. The projectile will just continue on forever. Can anyone spot the cause?

Currently my console is saying that getFrame and setFrame are unknown commands within the TakesDamageBehavior and cloneWithBehaviors is an unknown command in DealsDamageBehavior.

The console also is unable to find object when attempting to call function 'explode' and 'safeDelete'.

Thank you very much for any insight, I've been staring at this code for so long and my eyes are starting to hurt. Heh just cant figure this one out.

#1
09/12/2008 (7:57 pm)
Just for future reference you can put code blocks inside the tags [ code ] myCode [ /code ] (remove spaces).

When you are referencing "getFrame", "setFrame" and "cloneWithBehaviors", you are referencing the behavior and not the actual owner of the behavior.

if (%this.deleteOnHit)
{
    %this = %this.cloneWithBehaviors();
    %this.explode(getWords(%contacts, 0, 1));
    echo("Start safe Delete");
    %this.owner.safeDelete();
    echo("End safe Delete");
}

The "onCollision" function is defining the variable "%this" as the source of the collision, but then you attempt to clone the same object and override the value of "%this". I suspect that this is something that you do not want to do!

Try this:

if (%this.deleteOnHit)
{
    %this.explode(getWords(%contacts, 0, 1));
    echo("Start safe Delete");
    %this.owner.safeDelete();
    echo("End safe Delete");
}
#2
09/12/2008 (10:30 pm)
Hey Phillip, thanks for the future ref. Wasn't sure how to make the code blocks. ok so i tried your suggestion and the console started to clear up. I noticed that when I fired the projectile, that the owner was deleted and if I fired again it would actually take a copy of my entire background and shoot it. To stop this I replaced "%this.owner.safeDelete();" with "%this.safeDelete();". Now I'm able to fire correctly again but the projectiles are still passing through everything it collides with.
#3
09/14/2008 (10:33 pm)
Hmm I still cant figure this thing out. Projectiles are still colliding with objects and continuing through them.

if (!isObject(DealsDamageBehavior))
{
   %template = new BehaviorTemplate(DealsDamageBehavior);
   
   %template.friendlyName = "Deals Damage";
   %template.behaviorType = "Damage";
   %template.description  = "Set the object to deal damage to TakesDamage objects it collides with";

   %template.addBehaviorField(strength, "The amount of damage the object deals", int, 100);
   %template.addBehaviorField(deleteOnHit, "Delete the object when it collides", bool, 0);
   %template.addBehaviorField(explodeEffect, "The particle effect to play on collision", object, "smoke", t2dParticleEffect);
}

function DealsDamageBehavior::onBehaviorAdd(%this)
{
   %this.setCollisionCallback = true;
   %this.setCollisionActiveSend = true;
   %this.setCollisionActiveReceive = true;
}

function DealsDamageBehavior::dealDamage(%this, %amount, %victim)
{
   %takesDamage = %victim.getBehavior("TakesDamageBehavior");
   if (!isObject(%takesDamage))
      return;
   
   %takesDamage.takeDamage(%amount, %this);
}

function DealsDamageBehavior::explode(%this, %position)
{
   if (isObject(%this.explodeEffect))
   {
      %explosion = %this.explodeEffect.cloneWithBehaviors();
      %explosion.position = %position;
      %explosion.setEffectLifeMode("Kill", 1.0);
      %explosion.playEffect();
   }
}

function DealsDamageBehavior::onCollision(%this, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
   %this.dealDamage(%this.strength, %dstObj);
   
   if (%this.deleteOnHit)
   {
	  %this.explode(getWords(%contacts, 0, 1));
	  echo("Start safe Delete");
	  %this.safeDelete();
      echo("End safe Delete");
   }
}

Iv'e noticed that my smoke particle that plays (when the projectile collides) only when I have deleteOnHit set to true. This seems a lil funky. Obviously this prob would go away if the projectile was deleting on initial contact but I though maybe this might shed some light on the problem for you more advanced users.