Vector is losing an entry when function has finished

I am implementing an Entity Component System and am having trouble when I am attempting to add a Component to an Entity. The structure of doing so is as follows:

Below will find an Entity with an ID of 0, and add a Position component to it.

m_entityManager->addComponentToEntityID(0, ComponentManager::POSITION);

The above is just a helper function to first find the Entity, then it will call the function below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    void EntityManager::addComponentToEntity(std::shared_ptr<Entity> entity, ComponentManager::ComponentType componentType)
    {
	    if (entity != nullptr)
	    {
		    switch (componentType)
		    {
		        case ComponentManager::POSITION:
			        entity->addComponent(m_componentManager->getUnassignedPositionComponent());
			        break;

		default:
			break;
            }
	   }
    }


This function will find an unassigned (free, available, not added to an Entity) Position component and return it.
1
2
3
4
5
6
7
8
9
10
11
    PositionComponent& ComponentManager::getUnassignedPositionComponent()
    {
	    for (unsigned int index = 0; index < m_positionComponentVector.size(); ++index)
	    {
		    if (m_positionComponentVector[index].isAssigned() == false)
		    {
			    m_positionComponentVector[index].assign();
			    return m_positionComponentVector[index];
		    }
	    }
    }


Finally, inside of Entity we have this function:

1
2
3
4
    void Entity::addComponent(Component& component)
    {
	    m_componentVector.push_back(component);
    }


The Entities are held inside of a vector inside of EntityManager, created as normal objects on the stack. This is the same for the Position components held inside of ComponentManager.

My aim is to keep all Components inside of ComponentManager and systems make request to ComponentManager to process a component in some way. I want to keep pointers or references for each Entity, to the Components that it owns.

My problem is, once Entity::addComponent() is finished, the m_componentVector vector no longer holds the Component that was added to it, like it is falling out of scope. I think the problem is somewhere I am passing a copy instead of a reference? I originally had the parameters as pointers but was playing around trying to fix it.




http://www.eelis.net/iso-c++/testcase.xhtml
A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem.


> the m_componentVector vector no longer holds the Component that was added to it
¿how are you testing that?


¿what happens if you don't find an available position?
(if( not m_positionComponentVector[index].isAssigned() ) always fails)
Last edited on
In Visual Studio I break after I make the call to
m_entityManager->addComponentToEntityID(0, ComponentManager::POSITION);

Here I can look at the vector of Entities that the EntityManager holds, look at the Entity with ID 0 and look at it's Component vector, and it's empty.

To answer your edited question, yeah that isn't handled correctly, I still have situations like that to deal with.
Last edited on
The idea that Mr. ne555 is eluding to is that we need to see more code. For instance, the constructor to your 'Entity' class and the constructor for your 'Component' class for instance. The section of code that uses this function etc. I'm probably missing a step, but I don't understand the "ComponentManager::getUnassignedPositionComponent()" function, a vector is a dynamically allocated class; what does this do?

If you change the "std::vector.push_back()" call to "std::vector.emplace_back()" do you see a change in the outcome?
Topic archived. No new replies allowed.