allow add component to return added component refenrefce

This commit is contained in:
2025-10-23 15:37:06 +02:00
parent a427fb7099
commit ef498ba210
2 changed files with 8 additions and 10 deletions

View File

@ -25,11 +25,15 @@ public:
Entity(const Entity& other) = default; Entity(const Entity& other) = default;
template<typename Type, typename... Args> template<typename Type, typename... Args>
void AddComponent(Args&&... args) { decltype(auto) AddComponent(Args&&... args) {
assert(m_entity != entt::null && "Entity is empty"); assert(m_entity != entt::null && "Entity is empty");
assert(m_scene && "Scene has not been assigned to the entity"); assert(m_scene && "Scene has not been assigned to the entity");
m_scene->m_registry.emplace<Type>(m_entity, std::forward<Args>(args)...); if constexpr (std::is_void_v<decltype(m_scene->m_registry.emplace<Type>(m_entity, std::forward<Args>(args)...))>) {
m_scene->m_registry.emplace<Type>(m_entity, std::forward<Args>(args)...);
} else {
return m_scene->m_registry.emplace<Type>(m_entity, std::forward<Args>(args)...);
}
} }
template<typename Type> template<typename Type>

View File

@ -53,20 +53,14 @@ public:
modelEntity.AddComponent<rotate>(); modelEntity.AddComponent<rotate>();
assert(modelEntity.HasComponent<mesh>() && "model doesn't have any mesh!"); assert(modelEntity.HasComponent<mesh>() && "model doesn't have any mesh!");
// Object* grass = Object::LoadFile("./assets/common/cube/cube.obj");
// const auto cubeEntity = scene->m_registry.create();
// scene->m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f));
// scene->m_registry.emplace<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
// Cube template (use shared object to avoid reloading 1000 times) // Cube template (use shared object to avoid reloading 1000 times)
std::shared_ptr<Object> cubeObj = std::shared_ptr<Object>(Object::LoadFile("./assets/grass_block/grass_block.obj")); std::shared_ptr<Object> cubeObj = std::shared_ptr<Object>(Object::LoadFile("./assets/grass_block/grass_block.obj"));
auto batchEntt = scene->CreateEntity(); auto batchEntt = scene->CreateEntity();
batchEntt.AddComponent<batch>(); auto& cubeBatch = batchEntt.AddComponent<batch>();
auto& cubeBatch = batchEntt.GetComponent<batch>(); // auto& cubeBatch = batchEntt.GetComponent<batch>();
batchEntt.AddComponent<mesh>(cubeObj); batchEntt.AddComponent<mesh>(cubeObj);
assert(batchEntt.HasComponent<batch>() && "batch doesn't have any batch component!"); assert(batchEntt.HasComponent<batch>() && "batch doesn't have any batch component!");
assert(batchEntt.HasComponent<mesh>() && "batch doesn't have any mesh component!"); assert(batchEntt.HasComponent<mesh>() && "batch doesn't have any mesh component!");
// auto cubeBatch = scene->m_registry.get<batch>(batchEntt);
// Generate 1000 random cubes // Generate 1000 random cubes
for (int i = 0; i < 1000; ++i) { for (int i = 0; i < 1000; ++i) {
auto cubeEntity = scene->CreateEntity(); auto cubeEntity = scene->CreateEntity();