amethyst / specs

Specs - Parallel ECS
https://amethyst.github.io/specs/
Apache License 2.0
2.49k stars 219 forks source link

Allow components to perform cleanup (with access to the World) when they are removed #723

Open LoganDark opened 3 years ago

LoganDark commented 3 years ago

Description

Allow components one last access to the world when they are deleted. This could be done by either:

  1. Allowing some sort of call inside of Drop that gets the encompassing World (unlikely, probably really unsafe, stinks of globals)
  2. Creating some sort of trait that acts like Drop but allows the component to access the world like a System does. Perhaps it could be named Cleanup or something?

Motivation

I have a component that represents ownership over some physics objects. When the entity holding that component is deleted, I want the physics objects to also be deleted. The problem is that inside Drop, I have no access to the physics world because it's a resource. :(

Drawbacks

Unresolved questions

None right now

LoganDark commented 3 years ago

wha twhy does enter submit the issue give me a minute...

Imberflur commented 3 years ago

I think the main barrier here is that typically other parts of the world can be borrowed mutably and immutably while removing components from a particular storage.

zesterer commented 3 years ago

I think the most effective way to do this is to have a zero-sized 'delete' marker component and a system that runs at the end of the tick to go through entities with this marker performing whatever operation you want to do with them and then deleting them.

LoganDark commented 3 years ago

I think the most effective way to do this is to have a zero-sized 'delete' marker component and a system that runs at the end of the tick to go through entities with this marker performing whatever operation you want to do with them and then deleting them.

maaaayybbeeee? It feels extremely awkward and very not right but I guess it would technically work?

zesterer commented 3 years ago

@LoganDark This is the most correct way to do this within the ECS paradigm. Zero-sized marker components are extremely cheap (they're usually completely allocation-freeze).