diff --git a/include/Engine/Core/EntityWrapper.h b/include/Engine/Core/EntityWrapper.h index 55b64c6f..e4be8d78 100644 --- a/include/Engine/Core/EntityWrapper.h +++ b/include/Engine/Core/EntityWrapper.h @@ -23,10 +23,12 @@ struct EntityWrapper static const EntityWrapper Invalid; bool HasComponent(const std::string& componentName); + bool Valid(); - ComponentWrapper operator[](const std::string& componentName); + ComponentWrapper operator[](const char* componentName); bool operator==(const EntityWrapper& e); - explicit operator EntityID(); + explicit operator EntityID() const; + operator bool(); }; #endif diff --git a/src/Engine/Core/EntityWrapper.cpp b/src/Engine/Core/EntityWrapper.cpp index 7b321a63..c3c5cf27 100644 --- a/src/Engine/Core/EntityWrapper.cpp +++ b/src/Engine/Core/EntityWrapper.cpp @@ -13,18 +13,41 @@ bool EntityWrapper::HasComponent(const std::string& componentName) return World->HasComponent(ID, componentName); } -ComponentWrapper EntityWrapper::operator[](const std::string& componentName) +bool EntityWrapper::Valid() +{ + if (this->World == nullptr) { + return false; + } + + if (this->ID == EntityID_Invalid) { + return false; + } + + if (!this->World->ValidEntity(this->ID)) { + this->ID = EntityID_Invalid; + return false; + } + + return true; +} + +ComponentWrapper EntityWrapper::operator[](const char* componentName) { if (World->HasComponent(ID, componentName)) { return World->GetComponent(ID, componentName); } else { - LOG_WARNING("EntityWrapper implicitly attached \"%s\" component to #%i as a result of a fetch request!", componentName.c_str(), ID); + LOG_WARNING("EntityWrapper implicitly attached \"%s\" component to #%i as a result of a fetch request!", componentName, ID); return World->AttachComponent(ID, componentName); } } -EntityWrapper::operator EntityID() +EntityWrapper::operator EntityID() const { return this->ID; } +EntityWrapper::operator bool() +{ + return this->Valid(); +} +