I have a header...
Components.h
namespace ComponentManager
{
void InitializeComponents(std::size_t numComponents);
void AddComponent(const Component& c, std::size_t position);
std::vector<Component> GetComponents();
}
... and the implementation:
Components.cpp
#include "Components.h"
std::vector<ComponentManager::Component> components;
void ComponentManager::InitializeComponents(std::size_t numComponents)
{
components.resize(numComponents, Component());
}
void ComponentManager::AddComponent(const Component& c, std::size_t pos)
{
components[pos] = c;
}
std::vector<ComponentManager::Component> ComponentManager::GetComponents()
{
return components;
}
In main.cpp, I make sure to initialize the components vector, and I add several components. Upon debugging each AddComponent(...) call, I see that the components are actually stored in the vector. However, when I call GetComponents(), the returned vector is different, as if the components were never stored.
What am I missing?
EDIT: Adding main.cpp (inside main() function)
//Inside EntityManager::Initialize(), I call ComponentManager::InitializeComponents()
EntityManager::Initialize(5000);
for (unsigned int i = 0; i < 10; ++i)
{
uint16_t handle = EntityManager::CreatePlayer(static_cast<float>(i), 50.0f * i);
//Inside EntityManager::AddComponent, I call ComponentManager::AddComponent(..)
EntityManager::AddComponent(handle, ComponentManager::POSITION_COMPONENT | ComponentManager::RENDERABLE_COMPONENT);
}
auto components = ComponentManager::GetComponents(); //this vector does not contain the elements that were added.