# Bullet pattern isn't behaving as expected

I have a boss that's supposed to continuously shoot five streams of bullets, each at a different angle. It starts off just fine, but doesn't seem to want to use its entire array of bullets. No matter how large I set the length of bulletList, the boss simply stops shooting after a couple of seconds, then pick up again shortly. Here's what I'm using to generate the pattern:

Vector3 direction = new Vector3(0.5f, -1, 0);

for (int r = 0; r < boss.gun.bulletList.Length; r++)
{
if (!boss.gun.bulletList[r].isActive)
{
boss.gun.bulletList[r].direction = direction;
boss.gun.bulletList[r].speed = boss.gun.BulletSpeedAdjustment;
boss.gun.bulletList[r].position = boss.position;
boss.gun.bulletList[r].isActive = true;
break;
}

}
direction = new Vector3(-0.5f, -1, 0);
//Repeat with four similar for loops, to place a bullet in each direction


It doesn't seem to matter if the bulletList length is 1000 or 100000. What could be the issue here?

EDIT: This is the code that removes a bullet, when it goes out of bounds. It is inside Bullet.Update()

if (position.X > 950 ||
position.X < -950 ||
position.Y > 570 ||
position.Y < -570)
isActive = false;


This is the code that removes a bullet on contact with the player ship:

for (int i = 0; i < boss.gun.bulletList.Length; i++)
{
if (boss.gun.bulletList[i].isActive)
{
boss.gun.bulletList[i].Update(timeDelta);

if (!ship.invincible)
{
bulletSphere.Center = boss.gun.bulletList[i].position;
if (bulletSphere.Intersects(shipSphere))
{
boss.gun.bulletList[i].isActive = false;
ship.health -= boss.gun.damage;
}
}
}
}


And just for safe measure, here's the code that draws the bullets:

for (int j = 0; j < boss.gun.bulletList.Length; j++)
{
if (boss.gun.bulletList[j].isActive)
{
Matrix bulletTransform = Matrix.CreateTranslation(boss.gun.bulletList[j].position);
DrawModel(boss.gun.bulletModel, bulletTransform, boss.gun.bulletTransforms);
}
}


EDIT: It would appear that a large number of bullets are getting "stuck" on the boss. That is, they draw at boss.position, and never move.

• What is the interval after which the boss is shooting bullets? And why don't you use dynamic list? Commented Sep 20, 2012 at 9:32
• The interval is currently TimeSpan.FromSeconds(.1). I'm not using a dynamic list because I'm worried that list operations might be too expensive. Commented Sep 20, 2012 at 9:37
• NVM see answer. Commented Sep 20, 2012 at 10:18
• Rather than repeat with four similar for loops you should have an outer for loop which goes through the possible directions (either stored in an iterable of some kind or using basic arithmetic to rotate by 90 degrees). Commented Sep 20, 2012 at 12:52
• Is there a reason why you are breaking out of your loop as soon as you find an inactive bullet? Wouldn't you want to loop through the bullets and find all inactive ones and re-activate them? Commented Sep 20, 2012 at 17:10

## 1 Answer

You must make sure that your bullet are inactive at some point again. Or else your loop won't work. If all bullets are "active" the forloop won't shoot any bullets.

Coupled with break; it will just jump out of your for loop. ( I guess this is meant for the interval ? )

( But then again, I have the feeling you are leaving out some crucial code )

I suggest using a Stack or ObjectPool that just pops the top element and returns an inactive bullet. Once a bullet goes inactive ( after being activated) it's pushed back on the stack. This way you don't need to loop trough your bullet list to see if any of the bullets are inactive. You just check if something is in the stack. It's much faster.

C# has its own stack class. But creating one for your own needs isn't hard.

As an addition you could wrap your bullet patterns into a class which holds the direction for each bullet, the interval time( perhaps per bullet), speed(perhaps per bullet) etc. And when it's time to shoot the next bullet you simply request the next bullet settings from your "BulletPattern" class. This might prevent you writing giant pieces of codes just for one object. If you wrap the data you can also reuse/share them for/with other entities and such.

• The bullets do go inactive, upon either colliding with something or going outside the visible area. However, I'll give it a try with a stack. Arrays have been fine for me up to this point, since this is the most bullets I've ever pumped out. Commented Sep 20, 2012 at 13:39
• Have you tried putting a simple trace/print in your forloop? To see if it actually reaches those lines of code? Commented Sep 20, 2012 at 14:31
• Yes, I have. The strange thing is that it seems to not reach higher numbers in the for loop. Even if I set the size to 10000, it never gets past 500. Furthermore, it will continue counting while -not- firing. Commented Sep 20, 2012 at 15:11
• @Fibericon Then your problem lies elsewhere. Your submitted code isn't sufficient enough to tell us anything. The code seem fine, but I seriously suggest you implement a Stack to retrieve an inactive bullet. Your current design is bloated. Commented Sep 20, 2012 at 21:44
• Even if I set the size to 10000, it never gets past 500. Sounds like you have some kind of limiting effect somewhere. Try toggling Game.IsFixedTimestep. Does that cap change? Commented Sep 20, 2012 at 21:50