Some vectors help.

Hey, stuck using vectors again, could use some help.

I have a class called collectableHandler which creates multiple instances of class 'Collectable' in a vector.

A 'Collectable' has a bool called 'pickedUp' so I know when it has been collided with.
I want to be able to loop through the vector of 'Collectable' to check to see which ones have been collided with so I can remove them from the vector.

I would like to know how would I go about checking if each object has been collided with in the vector so I can set the bool to true.
I have at the moment one loop which checks against the x&y of a 'Collectable' but it doesn't seem to work with the vectors of 'Collectable'.

Some code below to try and help you understand me as im quite bad at describing!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
vector <Collectable*> v_collectables;

void CollectableHandler::render()
{
	for (it = v_collectables.begin(); it != v_collectables.end(); it++)
	{
		(*it)->Render();
	}
}

void CollectableHandler::update()
{
	for (it = v_collectables.begin(); it != v_collectables.end(); it++)
	{
		(*it)->Update();
	}
}
class Collectable
{

private:
	
	Vector2D m_Position; //the position of the 'Collectable' has an x & Y

         bool m_pickedUp; // set to false.

(Code below is in my game class)
if ((myPlayer.position().x == myPlayer.position().x) && (myPlayer.position().y == myPlayer.position().y))
	{
           myCollectable.pickedUp(true)
        }


edit***
1
2
3
4
5
6
7
8
9
10
11
void CollectableHandler::removeCollectables(int x, int y)
{
	for (it = v_collectables.begin(); it != v_collectables.end(); it++)
	{
		if (x == (*it)->position().x && y == (*it)->position().y)
		{
			v_collectables.erase(v_collectables.begin, v_collectables.end, (*it))
		}

	}
}


not sure how far im off, or if this is the right way, at the moment having some errors.
Last edited on
After you have erased from the vector the iterator will be invalidated, meaning it's not safe to increment it or use it in other ways. The erase function returns an iterator to the element that comes after the erased element. So you could do something like this:

 
it = v_collectables.erase(it);

This is still not enough because the iterator will be incremented right after so you miss to handle the element after the erased element, or even worse if the erased element is the last element. So what you need to do is prevent the iterator from being incremented after an element has been erased. You can accomplish this by moving the increment to the else part of the if statement, like this:

1
2
3
4
5
6
7
8
9
10
11
for (it = v_collectables.begin(); it != v_collectables.end();)
{
	if (x == (*it)->position().x && y == (*it)->position().y)
	{
		it = v_collectables.erase(it);
	}
	else
	{
		it++
	}
}


Note that this will only remove the pointers from the v_collectables vector. The objects that the pointers point to will continue to exist.
Last edited on
Line 28 makes no sense. You're comparing two variables to themselves.
 
(myPlayer.position().x == myPlayer.position().x)

Both sides of the comparison operator are the same. You're always going to get a true result.
Ditto for y.

Did you mean?
 
(myCollectable.position().x == myPlayer.position().x)


at the moment having some errors

Post the full text of the errors.

Last edited on

Line 28 makes no sense. You're comparing two variables to themselves.
(myPlayer.position().x == myPlayer.position().x


Some times I type to fast for my own good, yes that's what I meant :P.
Now that is fixed that everything seems to work and I can hit multiple objects and their pointers will be erased from the vector.

Note that this will only remove the pointers from the 'v_collectables vector'. The objects that the pointers point to will continue to exist.

Should I also be removing the objects? Or is it the pointers remain which will no longer point to anything?

Another question, I instantiate an instance of the 'Collectable' class in my game.cpp.
When doing so it calls the default constructor, within that I have some variables and a call to a function which randomizes the x & y. T

The x&y are passed through a gotoxy function, however they are overridden for some reason when my 'CollectableHandler' spawns a new instance of a 'Collectable' which causes the X&Y to not be correct in the console window.

Ideally I don't want this to happen but I need to access some functions that are in the 'Collectable' class. I am not sure of a work around at the moment other than not to create an instance of 'Collectable' in my game.cpp and then all x&y's match up perfectly.

Thanks for the help so far, really appreciated, helps me understand where I am going wrong. ^^

Last edited on
Topic archived. No new replies allowed.