Small issue...

Started by
14 comments, last by zedzeek 18 years, 9 months ago
Hey there, this problem is a simple one, with a simple solution. I am just not aware of what methods would be best to solve this problem (still kinda new to some parts of C++). I've got a class of turret enemy in the sidescroller. It shoots bullets, which have a seperate bullet class. How would I best code it so that when the turret fires, it creates a new instance of the bullet class for me to work with. Also, how could I program a feature into the bullet where after it performs bullet.hitplayer() (where it does it's damage and removes itself from activity) it deletes itself from memory? This, of course, would most likely be performed in a function triggered by the end of bullet.hitplayer(), or done directly by bullet.hitplayer() at the end of the function. Thanks! -IV

-IVHumble Student

Advertisement
That's cool that you are making a sidescroller.

Allocating and deallocating memory takes up quite a bit of CPU time.

Do you think that your protagonist entity would only be able to fire a certain maximum number of bullets at one time? If so, you could allocate the entire array at once, and then simply keep track of them using two sets, one for the bullets that are in the air, and those that are in the cache.

That way you only have to process the bullets that are in the air, and when they strike an object, they are simply moved to the cache set.

As they are fired, they are moved from the cache set to the "in the air" set, where they are drawn to the screen, and are made to follow the rules of the physics governing that type of fire.

Generally the STL implementation of the set class will not deallocate memory if only a single element is removed (or added), so it will most likely save some CPU time due to this optimization.

Just an idea. :)

If you do decide to go with this method, one other point should be made regarding optimization:

If your bullet object contains any bool member variables, it is better to make an array of vector<bool>. Create one vector<bool> for each of the bool members in the bullet class, each vector<bool> containing the same number of elements as the combined bullet sets sizes.

Due to the contiguous nature of the vector's memory layout, each bool element is made to take up only a single bit of memory (versus 1 byte on a stand-alone bool). You end up with a 7/8th savings on memory, which can be big.
You could also look into using memory pools. These little dittys are great for allocating a bunch of small objects once, and then you just draw from the pool when you need them and toss them back when you're done.

In the case of a bullet, when it is fired, you could grab a fresh bullet object from the pool, change its state to active, and start updating its position, etc. When it has gone off screen or has hit a target, you can toss it back into the pool.
Quote:Original post by SanityAssassin
You could also look into using memory pools. These little dittys are great for allocating a bunch of small objects once, and then you just draw from the pool when you need them and toss them back when you're done.

In the case of a bullet, when it is fired, you could grab a fresh bullet object from the pool, change its state to active, and start updating its position, etc. When it has gone off screen or has hit a target, you can toss it back into the pool.


So can you tell me more about these memory pools?

That sounds like more of what I need. I won't, however, completely write-off what taby said. I'm open to all ideas.

-IV

-IVHumble Student

If you want to know more about memory pools, boost::pool has a basic explanation and a solid implementation of pooled memory allocation.
Lol, is there anywhere I can find that in a nutshell? It's a gigantic amount of information, and I'm sure for a task as seemingly small as mine, I'm not going to need all of it. Anywhere I can find a tutorial?

-IV

-IVHumble Student

This may be viewed as "hacky" by some, but you could just allocate 200 or so bullets, disable them all and then pick and choose when you need them. If you fire a bullet, it finds the first free bullet from the array and uses that.
__________________________________Peter Lewis ([email=me_AT_pjblewis.com]E-mail[/email] | Portfolio)
That was my initial thought, with two issues.

First, I would need to set one static bullet array for my whole game, because every instance of the class would be drawing from one big array.

Second, I don't know if it's efficient to create an array of 1000 bullets if I don't know how many the game will need. Is it? What do you think the best way to implement this idea would be?

-IV

-IVHumble Student

Quote:Original post by TraderJack
Second, I don't know if it's efficient to create an array of 1000 bullets if I don't know how many the game will need.

It's better than alloc'ing on the fly. This is essentially a greatly simplified memory pool, which is what you were seeking :)
__________________________________Peter Lewis ([email=me_AT_pjblewis.com]E-mail[/email] | Portfolio)
I've done stuff like this in the past for various projects, and my approach wasn't too complex.

What I usually do is to start of by allocated a fixed number of empty or uninitialised objects, say 128 for this example, and store them in a linked list. Then when I need an object, I take one off the list. When I'm finished, I put the object back on the list. If the list happens to be empty (i.e. I'm using all the objects), I create another block of empty objects for the list (usually enough so that the total number of objects is the next power of 2).

This topic is closed to new replies.

Advertisement