ECS with ordering

Hello! I’ve been implementing the ECS in Monogame.Extended and have run into an issue with how the entities are ordered. As I understand it, the entities are stored in a bag and that bag has no inherent order (first come, first serve) when it comes to processing them later for update or render systems. Is there a way I can specify how I want to have the entities ordered? The problem was made apparent when I created a UI window in game (a few entities are drawn) then i remove that list of entities and then re-add. On the second display, the order of the entities is different (my panel is drawn in front of my ui elements even though it was created first and so should be rendered behind my ui elements).

First set of entities drawn (correctly)

Second time drawn (incorrectly):

I’m not sure how else to solve this issue besides being able to specify the order of what i want rendered but I’m open to suggestions.

I think use texture depth layer
rather than use entities foreach order

I didn’t think of that. I rolled my own by adding a component with a layer order and then rendering them based on that layer order. I assign the layer order at creation but i can easily switch the order if i needed it to as well. I’ll have a look at this too just in case it suits my needs.

I’d suggest that you implement your own layer system with ordering. If you use

SpriteBatch.Begin(SpriteSortMode.BackToFront, null);

then you can pass a depth value to each texture, but that will cause MonoGame to sort your textures at every frame, which you really don’t want.

You can do something like did:

Specify a depth to each layer, assign entities (textures) to that layer, and just draw the layers in the order of their depths.
This way, you can use SpriteSortMode.Deferred and there is no need for sorting when you render. (Unless you are developing a 2D game where you need Y sorting, but you can also do that with layers by sorting only the affected textures instead of letting MonoGame to sort all of your textures)

I think draw order is determined by order of systems added by the WorldBuilder. First come first served.

I.e. add the HUD stuff last.