Make the AddComponent function safe

This commit is contained in:
Maple Redleaf
2025-11-10 11:48:33 -06:00
parent f99daa2f9d
commit 03c3a75232
2 changed files with 7 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
#ifndef VOID_ENTITY_H
#define VOID_ENTITY_H
#include "component.h"
#include <memory>
#include <vector>
class Entity {
@@ -15,7 +16,7 @@ public:
void Destroy();
private:
std::vector<Component> components;
std::vector<std::unique_ptr<Component>> components;
};
#include "../src/entity_i.cpp"

View File

@@ -2,8 +2,12 @@
#ifndef VOID_ENTITY_H
#include "../include/entity.h"
#endif
#include <memory>
#include <utility>
#include <vector>
template <typename CompT, typename... Args>
Component *Entity::AddComponent(Args &&...args) {
return components.emplace_back(std::forward<Args>(args)...);
components.push_back(std::make_unique(std::forward<Args>(args)...));
return components.back().get();
}