Jump to content
  • Advertisement
Sign in to follow this  
DrunkenBrit

Array or vector of bullet objects?

This topic is 4519 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
Advertisement
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 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;
}

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!