diff --git a/libs/ecs/include/psemek/ecs/entity_accessor.hpp b/libs/ecs/include/psemek/ecs/entity_accessor.hpp index 10adf552..075ee83e 100644 --- a/libs/ecs/include/psemek/ecs/entity_accessor.hpp +++ b/libs/ecs/include/psemek/ecs/entity_accessor.hpp @@ -14,7 +14,7 @@ namespace psemek::ecs {} template - Component & get() + Component * get_if() { util::uuid const uuid = Component::uuid(); @@ -22,12 +22,26 @@ namespace psemek::ecs for (std::size_t i = 0; i < component_uuids.size(); ++i) if (uuid == component_uuids[i]) - return *reinterpret_cast(table_->get_component_pointers()[i].data + detail::stride() * row_); + return reinterpret_cast(table_->get_component_pointers()[i].data + detail::stride() * row_); + return nullptr; + } + + template + Component & get() + { + if (auto ptr = get_if()) + return *ptr; assert(false); __builtin_unreachable(); } + template + bool contains() const + { + return get_if() != nullptr; + } + private: detail::table * table_; std::uint32_t row_;