Skip to content

Commit

Permalink
Fixed a bug in deleteAllEntities beeing stuck with shouldBeKeeped ent…
Browse files Browse the repository at this point in the history
…ities and added a safeDelete function
  • Loading branch information
Eldriann committed Jun 24, 2019
1 parent 1bb653c commit 50423f7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ bool deleteEntity(const internal::ID &entityID);
```
> **INFO**: For this and the following functions please see the [documentation](#documentation) or the [examples](#examples) for details on how to use them.
If you are in an applyToEach loop or if you are iterating on entities better use safeDeleteEntity:
```cpp
void safeDeleteEntity(const internal::ID &entityID);
```
> **INFO**: For this and the following functions please see the [documentation](#documentation) or the [examples](#examples) for details on how to use them.
> **INFO**: You will need to call in a safe spot (like your main loop the function `applySafeDelete` to take effects of safeDelete)
The function used to retrieve an entity by its identifiers are as follow:
```cpp
EntityHandler getEntityByID(const internal::ID &entityID);
Expand Down
1 change: 1 addition & 0 deletions sources/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jf::entities::Entity::~Entity()
{
for (auto &i : _components) {
delete i.second;
i.second = nullptr;
}
events::EventManager::getInstance().emit<events::EntityDestroyedEvent>({this});
}
Expand Down
28 changes: 23 additions & 5 deletions sources/EntityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jf::entities::EntityManager &jf::entities::EntityManager::getInstance()
}

jf::entities::EntityManager::EntityManager()
: _maxId(0), _entities(), _freeIDs()
: _maxId(0), _entities(), _freeIDs(), _toDestroyIDs()
{

}
Expand Down Expand Up @@ -111,8 +111,26 @@ std::vector<jf::entities::EntityHandler> jf::entities::EntityManager::getEntitie

void jf::entities::EntityManager::deleteAllEntities()
{
while (!_entities.empty()) {
if (!_entities.begin()->second->shouldBeKeeped())
deleteEntity(_entities.begin()->first);
auto entity = std::find_if(_entities.begin(), _entities.end(), [](const std::pair<internal::ID, Entity *> &pair) {
return !pair.second->shouldBeKeeped();
});
while (entity != _entities.end()) {
deleteEntity(entity->first);
entity = std::find_if(_entities.begin(), _entities.end(), [](const std::pair<internal::ID, Entity *> &pair) {
return !pair.second->shouldBeKeeped();
});
}
}
}

void jf::entities::EntityManager::safeDeleteEntity(const jf::internal::ID &entityID)
{
_toDestroyIDs.push_back(entityID);
}

void jf::entities::EntityManager::applySafeDelete()
{
for (auto &id : _toDestroyIDs) {
unregisterEntity(id);
}
_toDestroyIDs.clear();
}
12 changes: 12 additions & 0 deletions sources/EntityManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,23 @@ namespace jf {
* @return true if the entity existed and was deleted false otherwise
*/
bool deleteEntity(const internal::ID &entityID);
/*!
* @brief Delete an entity given it's id
* @param entityID The id of the entity to delete
* @return true if the entity existed and was deleted false otherwise
*/
void safeDeleteEntity(const internal::ID &entityID);

/*!
* @brief Delete all the existing entities except the ones marked as shouldBeKeeped
*/
void deleteAllEntities();

/*!
* @brief Delete entities that have been marked for safe delete.
*/
void applySafeDelete();

/*!
* @brief Get an entity given an ID
* @param entityID The id of the entity to get
Expand Down Expand Up @@ -150,6 +161,7 @@ namespace jf {
uint64_t _maxId; /*!< The last biggest existing entity */
std::unordered_map<internal::ID, Entity *> _entities; /*!< A map containing all the existing entities */
std::queue<internal::ID> _freeIDs; /*!< Ids destroyed and not used */
std::vector<internal::ID> _toDestroyIDs; /*!< Ids of entities to destroy */
};

template<typename C, typename... Others>
Expand Down

0 comments on commit 50423f7

Please sign in to comment.