Game Development Community

Countdown behavior (with damage) for everyone

by Michael K. · in Torque Game Builder · 12/15/2008 (4:44 am) · 4 replies

Hi,

here's a countdown behavior. It applies damage to an object when the countdown reaches 00:00.

So how could you use this behavior? Well, let's say you are making a platformer and you want to kill the player if he doesn't complete the level within 2 minutes... thats what this behavior is for. You can also use this as a base for a time counter behavior (with very minor changes).

If anyone finds any mistakes, please post them here and i'll update the behavior for everyone.

if (!isObject(TimeDamageObjectBehavior))
{
   %template = new BehaviorTemplate(TimeDamageObjectBehavior);
   
   %template.friendlyName = "Countdown till damage";
   %template.behaviorType = "Game";
   %template.description  = "Damage an object when the countdown reaches 00:00";

   %template.addBehaviorField(counterobject, "Countdown text object", object, "", t2dSceneObject);
   %template.addBehaviorField(countdown, "Countdown in seconds", int, 60);
   %template.addBehaviorField(damage, "The amount of damage to apply", int, 100);
   %template.addBehaviorField(damageobject, "The object to damage", object, "", t2dSceneObject);
   %template.addBehaviorField(sound, "Normal click sound", string ,"");
   %template.addBehaviorField(soundfast, "Fast click sound", string ,"");
}

function TimeDamageObjectBehavior::onBehaviorAdd(%this)
{
   %this.counterobject.text = "88:88"; 
}

function TimeDamageObjectBehavior::onLevelLoaded(%this, %scenegraph)
{ 
   %this.time = (%this.countdown * 1000);
   // create a global $timeleft (incase needed elsewhere)
   $timeleft = %this.time;
   %this.owner.setTimerOn(1000);
}

function TimeDamageObjectBehavior::onTimer(%this)
{
   $timeLeft = $timeLeft - 1000;

   if (($timeLeft/1000) >= 0) 
   {
	   if (($timeLeft/1000) > 10) // 10 seconds left lets the fast click sound play
	   {
	        if (isobject(%this.sound)) alxPlay(%this.sound);
	   } else {

	        if (isobject(%this.soundfast)) alxPlay(%this.soundfast);
		else if (isobject(%this.sound)) alxPlay(%this.sound);
	   }
   }
    
   if (($timeLeft/1000) < 0)
   {
      $timeLeft = 0;
   
      %this.owner.setTimerOff();

      %takesDamage = %this.damageobject.getBehavior("TakesDamageBehavior");
      if (!isObject(%takesDamage))
         return;
   
      %takesDamage.takeDamage(%this.damage, %this.damageobject);
   }
   
   if (!isObject(%this.counterobject))
      return;   
  
   %minutes = mFloor((($timeLeft / 1000) / 60));
   %seconds = (($timeLeft / 1000) % 60);
   if (%minutes < 10) %minutes = 0 @ %minutes;
   if (%seconds < 10) %seconds = 0 @ %seconds;
   %formatnumber =  %minutes @ ":" @ %seconds;
   
   %this.counterobject.text = %formatnumber; 
}

function TimeDamageObjectBehavior::stopTimer(%this)
{
   %this.owner.setTimerOff();
}

- Michael

edit: added the stopTimer function, because i thought it could be handy

#1
12/16/2008 (10:21 am)
I implemented the exact code and then had it kill a low health object thus ending the level and bumping it to a game overscreen.


The time works, and I now the basic takesDamageAdv works

now the problem I'm having is once I implement this into the game, the object doesn't want to take damage and I get the error

unable to find object ' ' attempting to call function 'getBehavior' I've tried editing it with the name of the behavior, but I'm more artsy than I am a coder and I've kinda got a blank on the problem.

Any ideas?
#2
12/16/2008 (10:31 am)
Okay I fixed the error, I accidently edited the behavaior part, now when I run the code again it still doesn't do anything, course there aren't any errors.

How do I tie in this code to the takes damage advance code? I't would help with making this into a level fail code!
#3
12/16/2008 (11:08 am)
Hi Night,

i created this for the normal takedamage behavior... i'll check the takedamageadvance behavior tomorrow when i get in the office :-)

I think takedamageadvanced is build up quite differently if i recall correctly.

- Michael
#4
12/16/2008 (12:26 pm)
%takesDamage = %this.damageobject.getBehavior("TakesDamageBehavior");

Found my problem, the above TakesDamageBehavior calls upon the original

So I changed the above to call the TakesDamageAdvBehvaior datablock

%takesDamage = %this.damageobject.getBehavior("TakesDamageAdvBehavior");


Now it works perfectly! Thanks for all your help!