Game Development Community

Help with a loop

by Ryan Jones · in Torque Game Builder · 01/17/2009 (6:30 pm) · 3 replies

I'm trying to setup a loop so a gun enplacement is continually scanning for enemies in the area. Once a player gets within a certain range it raises up, fires, then lowers back down. Once down it goes back it resets itself looking for enemies again. The main problem is the schedule functions to pause between the animation of it lowering, firing, then lowering back down.

Here is the code I have in place, any help is appreciated:

function SecurityBitBehavior::scanningarea(%this)
{

// Get the positions
%cameraPosition  = sceneWindow2D.getCurrentCameraPosition();
%spawnPoint		 = %this.Owner.Position;
MaxSpawnDistance = 60;
	
// Find the distance to the camera
%distanceToCamera = VectorDist(%cameraPosition, %spawnPoint);
		
// Try to attack the player
//if (%distanceToCamera < %this.MaxSpawnDistance)
	%this.beginAttack();
}

function SecurityBitBehavior::beginAttack(%this)
{
//Animation for the gun to raise for attacking
	%this.owner.playAnimation(securitybitraise);
	%this.schedule(2000, "SecurityBitFire", 0);		
}

function SecurityBitBehavior::SecurityBitFire(%this)
{
//This gets the bullet object 
   %this.projectile = "securitybitshot";
   %projectile = %this.projectile.cloneWithBehaviors();
   %projectile.setPosition(SecurityBit.position);
   %projectile.setLinearVelocityPolar(90, 100);

   %this.schedule(1000, "reset2Down", 0);
}

function SecurityBitBehavior::reset2Down(%this)
{
//Animation for the gun to lower back down after finish attacking
   %this.Owner.playAnimation(securitybitlower);
}

#1
01/18/2009 (7:36 am)
This may be easier to set up in the behaviors onUpdate funtion. Something like this.

function SecurityBitBehavior::onUpdate(%this)
{
    // Get the positions
    %cameraPosition  = sceneWindow2D.getCurrentCameraPosition();

    // Find the distance to the camera
    %distanceToCamera = VectorDist(cameraPosition, %this.owner.Position);

    //check to see if player close
    if(%distanceToCamera < %this.maxSightDistance  && !%this.attacking)
    {
       %this.beginAttack();
       %this.attacking = true;
    } else if(cameraDis >= maxSightDistance  && %this.attacking)
    {
       %this.reset2Down();
    }
}

I used the %this.attacking to track if it's attacking, so only one event will be scheduld at a time. In you fire core you'll need to reset it.

function SecurityBitBehavior::reset2Down(%this)
{
   //Animation for the gun to lower back down after finish attacking
   %this.Owner.playAnimation(securitybitlower);
   %this.attacking = false;
}

#2
01/20/2009 (3:08 pm)
Ryan, I have done similar behaviors. Could you please give me an example of what happens? You say that there is a problem with the pausing of the schedule. So does that mean it scans fine, but never fires? or fires but never resets? Where is it breaking?
#3
01/21/2009 (7:25 am)
Hey Nic, Steve Hine directed me on the correct path so that's why I didn't respond right away. Thank you tons Steve! You helped me gain like two levels in my coding skill ;)

Here is the code I have incase somebody else is having a similar problem.

//Enemy emplacement Behavior
//With help from Steven Hine

if (!isObject(SecurityBitBehavior))
{
	%template = new BehaviorTemplate(SecurityBitBehavior);
	
	%template.friendlyName	= "Security Bit";
	%template.behaviorType	= "AI";
	%template.description	= "Handles AI of SecurityBit";
}

function SecurityBitBehavior::onAddToScene(%this, %scenegraph)
{
//Distance that AI will act from Player's position
     %this.maxSightDistance = 50;
//Variable states that the AI is not attacking by default
	%this.attacking = false;
}

function SecurityBitBehavior::onUpdate(%this)
{
    // Get the positions
    %cameraPosition  = sceneWindow2D.getCurrentCameraPosition();
    %enemyfocal	= %this.Owner.Position;

    // Find the distance to the camera
    %distanceToCamera = VectorDist(%cameraPosition, %enemyfocal);

    //check to see if player close
    if(%distanceToCamera < %this.maxSightDistance)
    {
//If we're currently attacking, then don't do anything
	if(%this.attacking)
		return;
       %this.attacking = true;
       %this.beginAttack();
    }
    else if(%distanceToCamera >= %this.maxSightDistance)
    {
	if(%this.attacking)
		return;
       %this.reset2Down();
    }
}

function SecurityBitBehavior::beginAttack(%this)
{

	%this.owner.playAnimation(securitybitrotate);
//This schedule is because I wanted the animation of the gun raised up
//up in place and wait for 2 seconds before firing
	%this.schedule(2000, "securitybitraise", 0);		
}

function SecurityBitBehavior::SecurityBitFire(%this)
{
//securitybitshot is the object name of this enemy's shot, an object that
//is placed outside of the camera's view
   %this.projectile = "securitybitshot";
   %projectile = %this.projectile.cloneWithBehaviors();
   %projectile.setPosition(SecurityBit.position);
//Meaning it will fire to the right at a speed of 100, left would be 270
   %projectile.setLinearVelocityPolar(90, 100);
}

function SecurityBitBehavior::reset2Down(%this)
{
//Gun enplacement lowers, attacking is set to false so can recheck if
//Needs to raise again
   %this.Owner.playAnimation(securitybitlower);
   %this.attacking = false;
}