-
dynamic reallocation
Ok, I'm working on a game with several guns that shoot bullets. Each gun class has a dynamic array of bullet objects (I know it should be a linked list) which is resized each time a new bullet is created with this function:
Code:
void gun_t::addbullet(float setx, float sety, float setth) {
int i;
bullet_t *temp = new bullet_t[numbullets];
for (i = 0; i < numbullets; ++i)
temp[i] = bullet[i];
delete [] bullet;
++numbullets;
bullet = new bullet_t[numbullets];
for (i = 0; i < numbullets; ++i)
bullet[i] = temp[i];
bullet[numbullets-1].set(setx,sety,setth);
delete [] temp;
}
So I set up the call to this function like this:
Code:
int temp = numbullets;
addbullet(x,y,th);
if (temp == numbullets) error("what?");
This error message shouldn't be displayed, but it is. Does anybody know why this is happening?
-
Well, just look at what you are doing
Code:
int temp = numbullets;
addbullet(x,y,th);
if (temp == numbullets) error("what?");
You don't pass temp into the function so it is not going to be modified. Of course it will still be equal to numbullets after the function call. You need to get the new number of bullets after the function call and then compare.
P.S. - All those allocations are going to be super slow if you are shooting a lot of bullets. You should look into making a memory manager to avoid all those small allocations. Or look into boost's library which provides a fast memory manager for just such things ( bullets, particles, etc. )
www.boost.org
-
i'm assuming this is a class, so the fact that he used a temp variable is correct. The numbullets variable is the one that SHOULD be changing, but it's not.
I'd suggest popping some error messages in to the addbullet function and making sure that it is actually called, also, put some errors at the front and the end of the function that output the numbullets variable, so you can see if it's being reset somewhere else for some reason
-edit-
oh yah, i'd look in to the realloc function for resizing your array of bullets, not only is it faster, but it's a lot cleaner too :d
http://nehe.gamedev.net/data/article...asp?article=05
look down near the bottom and look at the realloc function, if you've never used it before
-
> which is resized each time a new bullet is created with this function:
It would be far more efficient to either allocate for the max number of bullets, or allocate them in blocks of say 5 or 10 at a time.
> I know it should be a linked list
Yeah, because when you come to removing bullets, you'll see that they don't necessarily impact in the order in which you fired them.
-
one other way you could do it is to have a "peak" bullets, and as each bullet dies, just fill its spot in the array with a NULL pointer or something. Then, when you create a new bullet, just search for the first open slot in the list and pop the new bullet in there. If you don't find a new slot, then resize the array. This basically makes it so that over time the size of the array averages out pretty nicely. It would also be best to initially size the array to an amount that seems reasonable for your game, so you won't have to resize it unless a LOT of things are flying around.
But in the end, a linked list would of course function much better