Trouv / bevy_ecs_ldtk

ECS-friendly ldtk plugin for bevy, leveraging bevy_ecs_tilemap
Other
627 stars 73 forks source link

LDTK entity problem. #303

Open Gavolot opened 4 months ago

Gavolot commented 4 months ago

Perhaps I don’t understand something, but I have such strange seemingly problems:

1) I have no way of knowing to which level the entities “registered in LDTK” belong. And I really need this in order to somehow delete them when changing levels or exiting. I can enter a level once and delete it, but when I enter it a second time, it turns out I have to load the entire ldtk world, and at the same time somehow save what level we are moving to... It looks extremely strange.

2) Is it really true that all entities of all levels are created at once and only 1 time?

Trouv commented 3 months ago

Sorry it took me some time to get to this issue.

I have no way of knowing to which level the entities “registered in LDTK” belong. And I really need this in order to somehow delete them when changing levels or exiting. I can enter a level once and delete it, but when I enter it a second time, it turns out I have to load the entire ldtk world, and at the same time somehow save what level we are moving to... It looks extremely strange.

So, once an entity is spawned via the registration system, it will be a grand-child of the level entity in bevy's transform hierarchy. You can read more about the hierarchy of the LDtk world the plugin follows in this chapter: https://trouv.github.io/bevy_ecs_ldtk/v0.9.0/explanation/anatomy-of-the-world.html

So, actually, when a level despawns (recursively), it should despawn all of its children, which includes all of its entities. Is that not what you're observing? How are you despawning levels, are you using the level selection mechanisms provided by the plugin?

The exception to this is entities that you give the Worldly component, which will make it a child of the world entity rather than the level entity. The purpose of this component is to make it so worldly entities don't despawn when a level despawns, and also don't doubly-spawn if a level is is despawned/respawned. It's intended for entities that persist between levels, like a player.

Is it really true that all entities of all levels are created at once and only 1 time?

This is not entirely correct. It is true that all entities within a level are spawned at once. However, it's not necessarily the case that all levels are spawned at once - only the newly-selected ones in the previous update.

Also, entities are spawned only once when the level is spawned, but if that level is despawned/respawned then those entities will be despawned/respawned too. The exception to this, again, is entities given the Worldly component.

I hope this clears things up, but I'm not 100% sure I understood your questions completely.