aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Wicks <david@sosolimited.com>2015-01-14 12:52:27 -0500
committerDavid Wicks <david@sosolimited.com>2015-01-14 12:58:44 -0500
commit0f642da244f97b009cfa0b310e6914aaf78c1314 (patch)
tree6f0d347b4844717be9705a4fa2809b8dd56e67a6
parentd0c902c6167f965749b720d0c7ab9557187ec422 (diff)
Entity::replace<Component> method.
Avoid the client-side song and dance around assignment in cases where a component of the same type may already be assigned to the entity.
-rw-r--r--entityx/Entity.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/entityx/Entity.h b/entityx/Entity.h
index fa88726..85033c9 100644
--- a/entityx/Entity.h
+++ b/entityx/Entity.h
@@ -135,6 +135,9 @@ public:
template <typename C>
ComponentHandle<C> assign_from_copy(const C &component);
+ template <typename C, typename ... Args>
+ ComponentHandle<C> replace(Args && ... args);
+
template <typename C>
void remove();
@@ -865,6 +868,19 @@ ComponentHandle<C> Entity::assign_from_copy(const C &component) {
return manager_->assign<C>(id_, std::forward<const C &>(component));
}
+template <typename C, typename ... Args>
+ComponentHandle<C> Entity::replace(Args && ... args) {
+ assert(valid());
+ auto handle = component<C>();
+ if (handle) {
+ *(handle.get()) = C(std::forward<Args>(args) ...);
+ }
+ else {
+ handle = manager_->assign<C>(id_, std::forward<Args>(args) ...);
+ }
+ return handle;
+}
+
template <typename C>
void Entity::remove() {
assert(valid() && has_component<C>());