DefaultEcs icon indicating copy to clipboard operation
DefaultEcs copied to clipboard

add entity template

Open Doraku opened this issue 3 years ago • 5 comments

Useful to quickly create entities with the same composition Should add a way to state if created entity from the template should share component or not

Doraku avatar Jun 11 '21 08:06 Doraku

Thats exactly what i just wanted to task... in unity those are called archetypes. And its actually pretty usefull. Are there any plans yet ?

genaray avatar Oct 03 '21 20:10 genaray

This is different from unity's archetypes. Archetypes store all their component data next to each other, meaning that when you add or remove a component you need to move all the other components data to the new archetype of the entity. DefaultEcs currently use arrays of components to store the data. That means when you access multiple components on an entity you will jump around in memory which is slower than archetype (while having predictable memory access to help the cpu cache) but adding/removing a component only makes a change on this specific component array (simpler and faster).

What I meant by template here is to literally have an entity template, a special kind of entity which is not picked up by the queries but on which you can set components and later use to create real entities with the same composition to simplify some use case (like having an entity factory to create particle entities). I am not sure there is a real benefit yet, you could just as easily have a method to create your entity with all your needed components but I have seen it in other frameworks so why not. The other good point is that this template could be serialized so this "factory" becomes data editable outside of the game instead of being hard coded. I know exactly how to add this feature but it will take some times to implement properly (support for serialization, for EntityCommandRecorder, ...) and for now I am looking into adding the ability to have multiple component of the same type on one entity (which also needs a lot to work properly ^^").

As for real archetype, I don't see myself adding it in short or mid term, it is quite hard to do properly in pure c# because of memory management and the need to create archetype types on the fly. But the idea is still here :) I just want to keep things simple for now.

Doraku avatar Oct 04 '21 21:10 Doraku

This is different from unity's archetypes. Archetypes store all their component data next to each other, meaning that when you add or remove a component you need to move all the other components data to the new archetype of the entity. DefaultEcs currently use arrays of components to store the data. That means when you access multiple components on an entity you will jump around in memory which is slower than archetype (while having predictable memory access to help the cpu cache) but adding/removing a component only makes a change on this specific component array (simpler and faster).

What I meant by template here is to literally have an entity template, a special kind of entity which is not picked up by the queries but on which you can set components and later use to create real entities with the same composition to simplify some use case (like having an entity factory to create particle entities). I am not sure there is a real benefit yet, you could just as easily have a method to create your entity with all your needed components but I have seen it in other frameworks so why not. The other good point is that this template could be serialized so this "factory" becomes data editable outside of the game instead of being hard coded. I know exactly how to add this feature but it will take some times to implement properly (support for serialization, for EntityCommandRecorder, ...) and for now I am looking into adding the ability to have multiple component of the same type on one entity (which also needs a lot to work properly ^^").

As for real archetype, I don't see myself adding it in short or mid term, it is quite hard to do properly in pure c# because of memory management and the need to create archetype types on the fly. But the idea is still here :) I just want to keep things simple for now.

Alright thanks for the insight into default.ecs ! :D Such entity factores/cloneable entities would come in pretty handy and could save some time, so i actually think its a very good plan !

Besides that, more performance is always better ^^ but i can fully understand if "real" archetypes will not become a feature, the amount of work must be insane. Furthermore default.ecs IS already the fastest or the second fastest ( looking at LiteECS ) c# ecs out there. I ran a little movement simulation yesterday on my server and was able to process up to 8 million entities with constant 60 FPS ( ParallelRunner 4, only structs, usage of references )... this is just insane im not even sure if more is even possible :D

Keep up the great work !

genaray avatar Oct 04 '21 21:10 genaray

I found that the "CopyTo" method of Entity class has pretty much implemented the functionality of entity templates. We can create an entity, disable it, set its components on demand, and use it as a template. When there's need to create an entity from the template, just copy from this "template entity" and all is done. The only weakness is that it can't specify whether a component is copied from the template or referenced from the template.

XCYRKDSDA avatar Oct 30 '22 10:10 XCYRKDSDA

yep as you said I am also using CopyTo as a template standin for, but template would allow to use shared components and keep the enable/disable state of each component type, template could use one another to ease composition... when it will eventually come ^^"

Doraku avatar Oct 30 '22 11:10 Doraku