Keeping track of bullets

Bossa Nova
Post: #1
Well, not bullets really.

Here's my problem. I want my game characters to shoot energy attacks. I have a class hierarchy whereas every CCharacter class has a CAttack member. CAttack is responsible for drawing an attack and making it fly through space. Everytime the player clicks the mosue I'd like to make another CAttack and make it fly from the player. However, I dunno how to do this.

My current solution is very hack-ish. I make a huge array of CAttacks (50 actually --I need at least this many. This kills my memory usage BTW) and loop through them every frame calling an update function in CAttack that moves and redraws it. An bool called active keeps track of whether to actually call the update and draw function or not.

For every CCharacter i
for every j where j < 50
if(CCharacter[i].CAttack[j].active == true)

There must be a better way to do this. Can someone please, PLEASE help. There must be some elegant way to make attacks self-manage, create, and destroy themselves (not to mention passing data around for collision). I'm sure others have tackled this issue, how is it usually solved?
Quote this message in a reply
Post: #2
Try making a global array of CAttacks and just run through all of them each frame.

the C++ std::vector will make this very easy, you can just call:

#include <vector>
std::vector<CAttack> gAttacks;

CAttack attack;
gAttacks.push_back( attack );
Quote this message in a reply
Posts: 112
Joined: 2002.06
Post: #3
Use a linked list. If you want an example of how to do this, go to the Premier Press website ( and download one of the Mac Game Programming chapter source code files. There's two files, LinkedList.cp and LinkedList.h that implement a linked list. With those two files you will be able to do what you need: add bullets, remove bullets, and go through a list of bullets and update their movement.

Mark Szymczyk
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #4
I second the recommendation to use std::vector.

If it turns out that that's not efficient enough for your particular usage patterns, it'll be easy enough to switch to std::list later.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  how to track the last lines you modified Najdorf 10 5,787 Jun 11, 2006 11:24 PM
Last Post: Dan Potter
  Opinion on lots of bullets in game unknown 9 5,232 Oct 24, 2005 12:01 PM
Last Post: SethWillits
  Call For Help: The Game Track Open Source Project jolivierld 2 3,579 Aug 30, 2005 03:48 AM
Last Post: jolivierld