Vectors

Started by
16 comments, last by pundit 15 years, 3 months ago
I am very new to vectors, I don't have alot of time to learn a bit about them and implement it into my code. Heres the deal. I was thinking of using a vector to hold bullets for a plane. Have something like: int MAX_BULLETS [100]; vector <SDL_Surface> *BulletVect; for (int Min_Bullets < MAX_BULLETS; MAX_BULLETS != 0; Min_Bullets ++) { somehting like new bullet. ------------- BulletVect [MAX_BULLETS]; //Correct my syntax if you can please. delete Bullet; ------------- } I am drawing a blank though on how to make this work. I have a bullet img pointer ( SDL_Surface *Bullet_Surface ). Im just not sure how to make it so I can draw the bullets out of the vector on command (eg. case SDLK_SPACE : ). Like I said before, I am not familier with vectors in any case. I do know what push_back and pop_back do, but I don't know what they would do in this situation or why it would be nessesary. I do have a dead update and draw function. But I am still unclear on how to point the vector at the class and where to. Taking a wild stab at how to point it to the class: BulletVect *ExClass So help me make this work, thanks. [Edited by - MeritGamer on January 19, 2009 4:53:41 PM]
Advertisement
MeritGamer,

I know that you don't have a lot of time to learn this stuff, but the code you posted contains a lot of mistakes and things that don't make sense. Are you sure you understand the programming language well enough to take on an API like SDL?
Quote:Original post by CDProp
MeritGamer,

I know that you don't have a lot of time to learn this stuff, but the code you posted contains a lot of mistakes and things that don't make sense. Are you sure you understand the programming language well enough to take on an API like SDL?


I definatly know enough to take on SDL, and I definatly have the knowlege to make a simple game. But I am trying to expand my knowlege. Truth be told, this is for my final assignment and my partner is useless. He is not cut for programmign the least. This is my profesion and I cannot afford a bad mark. This is only grade 11 and only a C course, but I know I could do this. But I am always babysitting my partners code because if he doese code it is terrible, he doesnt listen and he doesnt solve things on his own.

So aside from that, I am trying to make a decent shooting rig that is bound to impress my teacher for a good mark. I realise I will not be able to understand it all in depth, but I plan to research after this nightmare is done in..2 days.
vector<SDL_Surface> bulletVect;
bulletVect.resize(100);

works exactly the same as

SDL_Surface *bulletVect = new SDL_Surface[100];

Now, the beauty of a vector is that you can do this:
vector<Bullet> bulletVect;//...if ( player_presses_fire ){bulletVect.push_back(Bullet());}//..for ( vector<Bullet>::iterator i = bulletVect.begin(); i != bulletVect.end(); ){  if ( !i->alive() )    i = billetVect.erase(i);  else    ++i;}

okay, first of all, i advise you to buy the following book asap: "The C++ Standard Library" by Josuttis. It has all the answers you dreamed of and more! (6 more containers like vector) also, you could find all of this by using google!

also, you should make your own homework and start your research way more early.

anyway, because you say you're going to do some research i'll keep you to that and i'll help you out a little here (heck, i've had my monkey years too)

Okay, so a vector is a template, so you can give the type it uses as a parameter like this: vector<type> vectorname so for example vector<bullet> m_bulletvector

Anyway, push_back pushes back an element at the back of your container(in this case, a vector) while pop_back removes the element at the end of the container
so we'll do m_bulletvector.push_back(nameofelement);
You can iterate through its elements using pointers like you do now, but you could and should use iterators, do some research on these too asap.

Anyway, i hope this helped a little..

edit: and KulSeran was just a tad faster
Well, can you explain what some of the code you posted is intended for, in that case? For instance, it looks like you want to have an int called MAX_BULLETS to store the maximum number of bullets that can exist (100, in this case), but what you've actually done is declared an array of ints with 100 elements. That's like creating 100 separate ints that can be accessed as follows: MAX_BULLETS[0], MAX_BULLETS[1], MAX_BULLETS[2], etc.

And even if that was your intention, your for loop doesn't make sense. First of all, you have a comparison in the initialization section of your loop. It's preceded by the keyword int, which seems to indicate that you want to create an int called Min_Bullets, but it won't compile because what you've written is a comparison, not an initialization.

And in the comparison section of your loop, you do have a comparison, but your comparison doesn't make sense for what you seem to want to do. That line will compile, but it will not do whatever it is you expect it to do.

Also, I seriously, seriously doubt that you want to create a vector of SDL_Surfaces. You don't need a separate surface for each bullet. Separate screen coordinates? Yes, but in general you only need one SDL surface to hold the image data for your bullet, even if you have multiple bullet types.

I'm not trying to bust your chops, but take it from me: it's really difficult to move on to an API if you don't have the language down pat.

Also, keep in mind that no one on here will help you with a homework assignment. I don't mind helping you with general programming concepts and syntax and stuff like that, but not the problem-solving aspects of your assignment.
Yes I am not asking anyone todo my assignment, jsut a general understanding of somethings.

Ok yes, I see what you mean. I created an array with a 100 elemnts, no that is not what i intended, i ahve no idea what I was thinking there.

int MAX_BULLETS = 100;

Yes, the vector is going to holld the value 100.

The for loop, you are right, I have no idea what I was thinking there either. Im thinking this is a bit more logical:

if (MAX_BULLETS == 0)
{
BulletVect.push_back [1];
}

Specifically, I want the vector pointer to point to my bullet class. So it will be poointing at my dead update and draw functions. Then put this vector code into a bool shoot() const; function. So if user clicks space or soemthing the code will draw a bullet from the vector use the show functiont o apply it to the screen and then it will be updated with the update function so it will move from its offsets then naturally the boolean dead is self explanitory.
Try starting by describing what you want the code to do. In detail. In English.

Do you have existing code that uses an array which you'd like to replace with a vector? If not, what are you hoping the vector can do for you?
"Specifically, I want the vector pointer to point to my bullet class. So it will be poointing at my dead update and draw functions. Then put this vector code into a bool shoot() const; function"

Okay, well, the funny thing is it is already 'pointing' toward your bullet class
As you use the bullets as elements, you see? Therefore you can also call the methods/member functions you declared for your class for each and every element of it in the vector. So in your case, you're just going to go through all the elements in the vector and you call your update and paint on each and every one of them.

Now if you want to shoot, you just push_back another bullet into your vector with the right parameters.
Quote:Original post by pundit
Now if you want to shoot, you just push_back another bullet into your vector with the right parameters.


Now when you say push back another bullet. I dont understand how that shoots it. So if it is being pointed at the functions, every value in the vector has the member functions already applied to it? And when you say right parameters, what do you mean?

This topic is closed to new replies.

Advertisement