Sign in to follow this  
TraderJack

Small issue...

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Just keep it simple:

Bullet bullets[100];

This gives you 100 bullets. Just put a flag in the bullet class that says whether a bullet is active or not. To delete a bullet, set it's active flag to false. To add a bullet, find an inactive one, set it's active flag to true and pop in your data.

Mike C.
http://www.coolgroups.com/zoomer/

Share this post


Link to post
Share on other sites
I have been working on a 3D shooter and i had this problem. The way i solved it was within the CObject class which i set up for all the game objects i tested for a collision with other objects including a bullet.

it was something like:

if(_pbsphere->TestCollision(this->_pgame->GetBullet()->GetBounding Sphere()))
{
this->_pbullet->SetPosition(100, 0, 0);
//this moves the bullet of screen
}

and within the CBullet class whenever i pressed the shoot button i would move it back to the centre of the camera with its set direction and velocity. Quite easy really!

Spencer

Share this post


Link to post
Share on other sites
not exactly what asked about but in my game lazers etc are treated as particle systems + not as a special object sort,
all PSs instances are created at game startup
each PS has a uniqueID each spaceshipo has a uniqueID, its very easy to send messages between the two (and not worry if one doesnt exist anymore)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this