Sign in to follow this  

Array or vector of bullet objects?

This topic is 4309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, can anyone weight up the pro's and con's of using an array of bullets rather than a vector and vice versa? Basically, I can set a bool to false once a bullet collides with another game entity, then that bullet will become unusable until the player collides with more ammunition in the world map, then set the bool back to true. Or, just keep track of how many ammunition packs the player collides with then i.e. IF AmmoCount != 0 && Trigger Pressed m_BulletVec.push_back(new Bullet()) END IF Thanks!:)

Share this post


Link to post
Share on other sites
Quote:
Original post by DrunkenBrit
Hi, can anyone weight up the pro's and con's of using an array of bullets rather than a vector and vice versa?

Basically, I can set a bool to false once a bullet collides with another game entity, then that bullet will become unusable until the player collides with more ammunition in the world map, then set the bool back to true.

Or, just keep track of how many ammunition packs the player collides with then i.e.

IF AmmoCount != 0 && Trigger Pressed

m_BulletVec.push_back(new Bullet())

END IF
You could do both: re-use an existing inactive bullet if one is available, else add a new one to the vector. That way you won't have to add new objects to the vector very often, but at the same time the number of bullets will be unlimited (practically speaking).

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
You could do both: re-use an existing inactive bullet if one is available, else add a new one to the vector. That way you won't have to add new objects to the vector very often, but at the same time the number of bullets will be unlimited (practically speaking).


It means that vector insertion becomes O(n), which may not be very practicable. I suggest using a std::list (because of its amortized constant time push_back()). Erasing an element also (amortized) constant time. Moreover, it is very rare to need a random access to a list of bullets, thus you won't need this nifty operator[] (most of the time, you'll use it in a loop; the same loop can be written using iterators, so you don't loose anything).

HTH,

Share this post


Link to post
Share on other sites
Quote:
Original post by DrunkenBrit
Okay, what you just said went right over my head :) But i'll do some research, thanks :)


Oh ! Sorry [embarrass]. I try to reword everything.

Instead of using a std::vector<> or a plain C array, I suggest you to use a std::list<>, for two main reasons:
  • the insertion is rather fast
  • the deletion is rather fast too

Of course, it is a list, meaning that you waon't be able to access a single item using the bracket [] operator (container[i] won't work). Anyway, it should not be a problem because most of the time you only use it in a loop

for (size_t i=0; i<array.size(); i++) {
data = array[i];
}

This loop can be reworked to use iterators instead:

for (std::list<MyType>::iterator it = list.begin(); it != list.end(); ++it) {
data = *it;
}


Of course, if you use the [] operator outside loops then using a std::list may not be a good solution.

I hope it is clearer :)

Share this post


Link to post
Share on other sites

This topic is 4309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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