Название описывает ситуацию, но основная проблема может быть чем-то совершенно другим.
Я пытаюсь создать простой игровой движок ECS.У меня есть куча сущностей, каждая из которых содержит vector<shared_ptr<Component>>
.Component
является базовым классом для всех производных компонентов.
Во время игрового цикла у меня есть различные System
, которые обновляют сущности определенными компонентами.Правильный компонент для системы получен с помощью следующей функции
template <typename T>
inline T& GetComponent(std::uint32_t type)
{
std::shared_ptr<Component> baseComponent = this->components[this->typeToIndexMap[type]];
std::shared_ptr<T> component = std::static_pointer_cast<T>(baseComponent);
return *(component.get());
}
. Проблема в том, что при попытке изменить компонент в системе (inputComponent.lastX = 5.f;
) он не работает.В следующем кадре значения совпадают.Я предполагаю, что происходит какое-то отключение, потому что я храню Component
, но не уверен, как решить эту проблему.Любые идеи, если это действительно так и как я могу решить?
Редактировать: Дополнительная информация
Сущность содержит компоненты в vector<shared_ptr<Component>>
Добавление компонента
void Entity::AddComponent(std::shared_ptr<Component> component)
{
this->components.push_back(component);
this->componentBitset = this->componentBitset | component->GetComponentType();
this->typeToIndexMap[component->GetComponentType()] = this->components.size() - 1;
}
Создание компонента
std::shared_ptr<InputComponent> inputComponent = std::make_shared<InputComponent>(InputComponent());
entity->AddComponent(inputComponent);
Структура компонента
class Component
{
public:
// Constructor / Destructor
Component(std::uint32_t type);
~Component();
std::uint32_t GetComponentType();
private:
std::uint32_t type;
};
Входной компонент
class InputComponent : public Component
{
public:
InputComponent();
~InputComponent();
double lastX;
double lastY;
};
Наконец, мы имеемсистема
void InputSystem::Update(GLFWwindow* window, std::vector<std::shared_ptr<Entity>>& entities)
{
for (int i = 0; i < entities.size(); i++)
{
if (entities[i]->IsEligibleForSystem(this->primaryBitset))
{
InputComponent component = entities[i]->GetComponent<InputComponent>(ComponentType::Input);
double x, y;
glfwGetCursorPos(window, &x, &y);
// some other things are happening here
component.lastX = x;
component.lastY = y;
}
}
}