Game Development Community

game slowdown and drop of framerate

by Edwin Rivera · in Torque Game Builder · 04/07/2011 (8:43 am) · 2 replies

Hi all,

I hope anyone can help me with this. I am working on a shoot'em up game and I am having some difficulty. I have about 8 "enemy ships" set up to the size (off view of the camera) and with the template behavior on. I have set up a sceneobject on the scene with a behavior which will activate these ships and bring them into the game. The problem occurs when I have more than 3 ships on screen. The game runs really slow and I am not sure what is causing this. all ships are using the timerShoot behavior.

This is the behavior I attached to the sceneobject.

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

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

%template.friendlyName = "enemyBehavior";
%template.behaviorType = "ai";
%template.description = "sets enemy ai";
}

function enemyBehavior::onAddToScene(%this, %scenegraph)
{

%this.schedule(2000, "wave1");
}
function enemyBehavior::wave2(%this)
{
blue1.teleport(20,20);
blue1.facePlayer();

// blue1.shootPlayer(0.25,80);
}
function enemyBehavior::wave1(%this)
{
yellow1.teleport(50,50);
yellow1.moveToPlayer(20);
yellow1.facePlayer();
//yellow1.shootPlayer(0.25,80);
green1.teleport(-10,20);
green1.moveToPlayer(30);
green1.facePlayer();
%this.schedule(2000, "wave2");
}

and this is my enemyShip class:
function EnemyShip::onLevelLoaded(%this, %scenegraph)
{
//variables to control AI
// Save the initial X value so we can use it when we respawn this ship
// %this.startX = %this.getPositionX();
// Missile speed
%this.missileSpeed = 0;
%this.fireRate = 0;
// Spawn this ship
%this.scenegraph = %scenegraph; // Make sure the scenegraph is available for the fireMissile() function
$scene = %scenegraph;
// Spawn this ship
// %this.spawn();
}

function EnemyShip::spawn(%this,%xp,%yp)
{
// Set the minimum and maximum Y positions to be the
// world limits, accounting for the size of the ship
// %this.minY = getword(%this.getWorldLimit(), 2) + (%this.getHeight() / 2);
// %this.maxY = getword(%this.getWorldLimit(), 4) - (%this.getHeight() / 2);

// Set the speed limits
// %this.minSpeed = -50;
// %this.maxSpeed = -100;
// %this.setEnabled(false);
// Set the speed and position of this ship
// %this.setLinearVelocityX(getRandom(%this.minSpeed, %this.maxSpeed));
//%this.setEnabled(false);
%this.setPositionY(%xp);
%this.setPositionX(%yp);

}
function EnemyShip::onWorldLimit(%this, %mode, %limit)
{
// When the ship hits the left side of the world limits, then respawn the ship
// if (%limit $= "left") {
// %this.spawn();
// }
}
function EnemyShip::explode(%this)
{
%explosion = new t2dParticleEffect()
{
scenegraph = %this.scenegraph;
};

%explosion.loadEffect("~/data/particles/big_explosion.eff");
%explosion.setEffectLifeMode("KILL", 1);
%explosion.setPosition(%this.getPosition());
%explosion.playEffect();
%this.spawn();

}
/////////////////////face player functions///////////////////////////
function EnemyShip::facePlayer(%this)
{
%this.faceObject = true;
%this.update();
}
function EnemyShip::stopFacePlayer(%this)
{
%this.faceObject = false;
%this.update();
}
/////////////////////move to player/object////////////////////////////
function EnemyShip::moveToPlayer(%this, %val)
{
%this.moveToObject = true;
%this.speed = %val;
%this.update();
}
function EnemyShip::stopMoveToPlayer(%this)
{
%this.moveToObject = false;
%this.update();
}
//////////////////////update function////////////////////////////////
function EnemyShip::update(%this)
{
if(%this.faceObject == true)
{
%vector = t2dVectorSub(player.position, %this.position);
%targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + 90 + 0.0;
%this.setRotation(%targetRotation);
}
if(%this.moveToObject == true)
{
%this.moveTo(player.position,%this.speed);
}
if(%this.shoot == true)
{
%this.fireMissile();
}
%this.schedule(1, "update");
}
function EnemyShip::fireMissile(%this)
{
if (!isEventPending(%this.fireSchedule))
%this.fireSchedule = %this.schedule(%this.fireRate * 3000, "createBullet");
/*%this.enemyProjectile = new t2dStaticSprite() {
scenegraph = %this.scenegraph;
imageMap = "redShipsImageMap";
frame = "4";
class = EnemyShip;
useSourceRect = "0";
sourceRect = "2.26805e-039 0 2.26805e-039 6.20121e-039";
canSaveDynamicFields = "1";
size = "2.186 1.929";
mountID = "38";
};

%this.enemyProjectile.setPosition(0,0);
//enemyProjectile.setRotation(%this.rotation);
//enemyProjectile.setLinearVelocityPolar(%this.rotation, 50);*/
}
function EnemyShip::shootPlayer(%this,%val1,%val2)
{
%this.shoot = true;
%this.fireRate = %val1;
%this.missileSpeed = %val2;
%this.update();
}
function EnemyShip::createBullet(%this)
{
%projectile = bullet.clonewithBehaviors();
%projectile.setPosition(%this.position);
%projectile.setRotation(%this.rotation);
%projectile.setLinearVelocityPolar(%this.rotation, 50);
}
function EnemyShip::teleport(%this,%val1,%val2)
{

%this.setPosition(%val1,%val2);
%this.spawn(%val1,%val2);
%spawning = new t2dParticleEffect()
{
scenegraph = %this.scenegraph;
};

%spawning.loadEffect("~/data/particles/energy_swirl.eff");
%spawning.setEffectLifeMode("KILL", 1);
%spawning.setPosition(%this.getPosition());
%spawning.playEffect();
%this.setEnabled(true);
}

thanks in advance to anyone who can help.

#1
04/09/2011 (11:27 am)
Please use [c0de] [/c0de] tags next time.

One performance issue I can see is
%this.schedule(1, "update");
within EnemyShip::update(). I don't think you really want to update your ships every millisecond.
#2
04/09/2011 (2:16 pm)
Hey Rpahut,

first off sorry for the block of code, will do right next time. I don't know how that slipped by me, yeah that seemed to have fixed th slowdown, thanks a lot. I have another question if you would, or anyone for that matter. How do I go about creating new ships based off the EnemyShip class, through script instead of the editor?