Skip to content
This repository was archived by the owner on Mar 12, 2021. It is now read-only.

Can't call activate() from within onEntityAdded. #77

Open
jyaif opened this issue Oct 31, 2016 · 1 comment
Open

Can't call activate() from within onEntityAdded. #77

jyaif opened this issue Oct 31, 2016 · 1 comment

Comments

@jyaif
Copy link

jyaif commented Oct 31, 2016

Found this problem while wanting to remove a component of an entity, when that entity was added to a new system.

Here's my understanding of the problem:
a. |onEntityAdded| is called while iterating over |m_entityCache.activated|.
b. Calling activate() leads to adding an entry to |m_entityCache.activated|.
Unfortunately, iterating on a vector that you modify doesn't work.

Possible fix:
Copy (or swap pointers/std::move) all the arrays over which you iterate at the beginning of World::refresh(), clear them, and only then iterate over them:

void World::refresh() {
  auto copyActivated = m_entityCache.activated;
  auto copyDeactivated = m_entityCache.deactivated;
  auto copyKilled = m_entityCache.killed;
  m_entityCache.clearTemp();
  for (auto& entity : copyActivated) {
@ninnghazad
Copy link
Contributor

Hm, just commented on #82 mentioning this. I thought having an assert to forbid calling activate from within refresh might do. it'd be simple to add, and if you (like me) still would want to activate entities from onEntityAdded, one would have to implement a temporary storage for those entities in own code. i'd prefer that cause i would want to be in control of when i actually need that overhead, as only a few refresh-calls actually have activates in them (at least for me).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants