Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't try to add/remove components of despawned entities #23

Merged
merged 2 commits into from
Jul 13, 2022

Conversation

derkork
Copy link
Contributor

@derkork derkork commented Jul 12, 2022

suppose you have some component FooComponent and BarComponent and you have a system like this:

foreach(var (entity, foo) in commands.Query<Entity,FooComponent>()) {
    if (entity.Has<BarComponent>()) { 
          entity.Remove<BarComponent>();
    }
    
    if (foo.IsFooTastic) {
         entity.Despawn();
    }
}

What now happens is both the remove and the despawn operation will be enqueued because for the duration of the enumeration the table where the entity is located is locked. When the enumerator is disposed the queued operations will be executed in reverse, so the despawn operation is first. This will remove the entity from the table. Next the remove operation runs, but it runs on an entity that is no longer alive.

There is probably a reason why these operations are executed in reverse so I kept this. Also just changing execution order would break as well if the despawn came first and the remove came later. So I chose to add a check to ApplyTableOperations to ensure that both adding and removing of components is only attempted if the entity is alive at this point in time.

@Byteron
Copy link
Owner

Byteron commented Jul 13, 2022

Thanks!

@Byteron Byteron merged commit e598011 into Byteron:main Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants