-2

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.
Francis Moy
  • 185
  • 9
  • 1
    You say "in main.cpp", but do you actually mean the `main` function or some global object's constructor in "main.cpp"? Because if it's the latter, it's an order of initialization problem, and we recently had exactly the same question. – Sebastian Redl May 03 '16 at 12:46
  • what is `Componet` ? – M.M May 03 '16 at 12:46
  • Why don't you post the contents of `main.cpp` as well ? They're pretty crucial to answering your question. – Quentin May 03 '16 at 12:49
  • Here's a possible dupe: http://stackoverflow.com/questions/7542054/global-vector-emptying-itself-between-calls – Sebastian Redl May 03 '16 at 12:51
  • @SebastianRedl I'm aware of that problem, but this is not my case; I'm actually calling from the main function, but thanks for the try. – Francis Moy May 03 '16 at 12:52
  • @M.M component is a data type (a union) that I define in ComponentManager namespace. I know the polemic around using unions and that I should be better using Variant, but the thing is that I can see that the vector is seemingly copying the components correctly, and I think I know what I'm doing in that respect... (hopefully not wrong... :) – Francis Moy May 03 '16 at 12:52
  • @FrancisMoy `Componet` (not `Component`) – M.M May 03 '16 at 12:59
  • Give us the freaking main or usage so that we can reproduce – Tezirg May 03 '16 at 13:01
  • @M.M Sure, sorry for the typo, and thanks for pointing out. I think it's now corrected... – Francis Moy May 03 '16 at 13:04
  • @Quentin I just edited the question in order to add an excerpt of the main.cpp. I don't want to tangle the question up with too much (apparently) useless code, but I hope this gives more information. – Francis Moy May 03 '16 at 13:08
  • Nope, time for a [MCVE]. – Lightness Races in Orbit May 03 '16 at 13:20
  • 1
    I agree for the mcve time @LightnessRacesinOrbit. I still want to ask for the ComponentManager::Component operator= and copy constructor implementation I think the error might be there – Tezirg May 03 '16 at 13:22
  • Wow, I didn't expect the question to be poorly rated. Sorry if it is confusing. I just thought that maybe I was missing something obvious with the code that I provided, but now I see that the problem might be deeper. As soon as I can, I'll first re-edit the question to fulfill @Tezirg request (Component code) and I'll try to provide a minimum code that you can reproduce. Again, sorry if I wasted your time. – Francis Moy May 03 '16 at 16:16
  • @Tezirg you were certainly in the right direction. The problem was deeper and had its roots in the definition of Component. My apologies to all of you who tried to answer for the misleading information. – Francis Moy May 05 '16 at 17:41
  • @Francis Moy. That's exactly what I was expecting. I suspected that the return copy of the vector wasn't performed in a proper way – Tezirg May 06 '16 at 08:15

1 Answers1

1

Originally, this was the definition of Component:

union Component
{
    PositionComponent positionComponent;
    VelocityComponent velocityComponent;
    AccelerationComponent accelerationComponent;
    RenderableComponent renderableComponent;
    Component() {}
    ~Component() {}
    Component(const Component& other) {}
    Component& operator=(const Component& other)
    {
        std::memmove(this, &other, sizeof(other));
        return *this;
    }
};

Adding the definition for the constructor and for the copy constructor was the fix:

union Component
{
    PositionComponent positionComponent;
    VelocityComponent velocityComponent;
    AccelerationComponent accelerationComponent;
    RenderableComponent renderableComponent;
    Component() 
    {  
        std::memset(this, 0, sizeof(Component)); 
    }
    ~Component() {}
    Component(const Component& other)
    {
        std::memmove(this, &other, sizeof(other));
    }
    Component& operator=(const Component& other)
    {
        std::memmove(this, &other, sizeof(other));
        return *this;
    }
};
Francis Moy
  • 185
  • 9