well
vector are good for
Accessing individual elements by their position index (constant time).
Iterating over the elements in any order (linear time).
Add and remove elements from its end (constant amortized time).
So if the bullets are not removed from the end it might just be slow
consider limiting the number of bullets and storing them in a
statically allocated structure to minimize memory overhead
post some moar code on pastebin so we can laugh at/criticise you
maybe some of it will be constructive
Name:
Anonymous2011-12-05 9:51
>>3
What would be a better method for handling my bullet clean up? Initially I assumed you meant something like this:
bullets.erase(bullets.end() - bullets[bi].ID);
but obviously that's essentially the same thing.
I was thinking about using an std::map<int, Bullet> object, and keep adding one to the ID each time instead of setting the ID to bullets.size();
bullets.erase(bullets.begin() + bullets[bi].ID);
doesn't this imply that the bullet.ID == bi
are you sure you can guarantee that when spamming
bullets.begin()+ bi might be better
if this loop looks like
for(vector<Bullet>::iterator bi;bi!=bullets.end();++bi)
{
if(something)
bullets.erase(bi);
}
>>5
it implies that bullets.push_back and bullets.pop_back are really fast lol
you are probably interested in linear iteration time and fast removal so map might be good if you have unique ids, lists if if don't have ids >>6
should be vecotr<Bullet>::iterotor bi=bullets.begin()
Name:
F r o z e n V o i d !!mJCwdV5J0Xy2A212011-12-05 10:04
OOP bullets? SO when you fire a bullet you create an object, with all the properties of the bullet, like speed/weight, which bullet factory and gun was used on it, maximum firing range,etc. You may need an extra layer of OOP handling to safely initiliaze,check and fire each bullet produced by BulletFactory,after awhile you query BULLETSQL databases(optimized for bullets, no bloat), scaling bullet production into extra servers and resynergizing the BulletCloud using latest metatemplates from stackoverflow to produce bullets at ludicrous speed.
class Bullet {
unsigned short mass
unsigned short volume
Bullet();
~Bullet()
};
And not be a little bitch about it. OOP isn't about how many features you can use and abuse. Use what you need, be more abstract when making libraries, don't be a bitch.
ITT:OOP 101
you can't into OOP if you never made your own silver bullet
Name:
Anonymous2011-12-05 15:22
>>1
The problem I see with using the ID value that you are, is that if I add 2 bullets to the list (IDs 0, and 1 respectively), and then the first bullet goes off screen / gets erased, then my list now has 1 single bullet with ID 2.
Are you updating each bullet's ID to its new position after you erase any others?
Name:
Anonymous2011-12-05 16:29
It seems you are trying to do things in unnecessarily complicated way.
Why do you need to store some array position into the bullet? If you are iterating over the vector, you will know anyway the index of the current bullet, and as >>17 pointed out, the indices will be invalid after you delete some element from the middle.
Anyway, I'd suggest using std::remove for removing your bullets. You can use it like this:
std::vector<Bullet>::iterator last = std::remove(bullets.begin(), bullets.end(), bullet_not_visible);
bullets.erase(last, bullets.end());
This is much more efficient than using bullets.erase for single bullets if you need to remove many bullets.