How to change Archetype Of Entity

I have some Entity and in one time I want to change it Archetype to specific one.
I can do that by adding components one by one than removing one by one than check to remove some unknown additional components but this all looks bad and performance heavy because of many entity moves between archetypes.

What I really want is just be sure to add/remove all components in one go so entity will become of specific archetype. Something like:

entityManager.MoveEntityToArchetype( entity, myLovelyArchetype );

How can I do that?

1 Like

What is your use case for such a functionality?

I have reuse entities for guarantee entity ids locally and across client/server and just add remove components on it.

So All reusable Entities pre created with empty tag and then moved to Some archetype when created and back when destroyed

Why not just let the system handle that?

How can it do this using provided Entity Id?

it is mostly ok to entirely destroy ad recreate entity if it keeps their id :slight_smile:

But how to do that?

Another part is sync entity across network.

When I receive new entity state from network today I need to read all necessary components from received data (Archetype), make sure my local entity have same archetype by manually add/remove components on it and slowly move it archetype to destination. and only then set each component on entity from received data.

with .MoveEntityToArchetype( ) all this boilerplate will gone and entity will move exactly once.

Currently the closest to your requirement is using the ComponentGroup overload of various EntityManager method. It’s not for single entity but that’s why it is possible to not move any data. If you can first find some way to separate them (e.g. add component tag) then from that point everything could be fast.

OP, can you create simply entities with desired archetype and sync it as suggested? How many entities we talk about.,How. Many components To be removed from initial entity and how many to be added, to match new archetype.

For me seams, like just having entity pair, with component that has relevant entity reference, with other archetype.

As I understand ComponentGroup overrides in not about adding many Components onto one entity but about adding one component on to many entities.
So it is not what i’m looking for :slight_smile:

There is method to add many compoents in one go but it work not good too because it throw exception of one of components already on entity

Creating new entities every time is exactly the thing I with to avoid. Not because overhead but but because boilerplate I need to write to keep many entities in sync across different machines.

We already have Good entity ids and creating additional layer of collections just to create separate identifiers and then support that thing and make random search to lookup something looks ugly.

I think I need no more than 1000 entities and I need to delete about 5 components and add about 10.
This is about performance and about simplicity of such operations, just want to use underlain Entity architecture rather ignore it.

I need something like create new record in archetype but use existing Id for it and remove old from old archetype.

Any particular reason, why you need and add components, instead creating all required components at entity creation? You are loosing this way an opportunity, to utilize effectively burst.
Seams like over complicating things.
You can simply use component as tag (s), to filter what you need, if you must.

Filtering there will not help. there is no things that can be filtered.
Reasons few posts earlier.

I short. I dont need to add components I just want to create entity with exact archetype and existing entity ID.
Is there some method to do so?

Ah that makes more sense.

The thoughts I got, coming to my mind in this right moment, is to create 1000 required (reserved) entities at beginning of the game (as an example) and reuse them. Then you can ensure, you have same set of entities across network clients (same IDs). Just like polling.

As a side note I don’t think Entity is for client server synching purpose? From the source code I see EntityGUID component popping up in various place. I am just guessing it might be intending to serve your use case. (EntityGUID is composed of 2 long)

1 Like

This is exactly what I did. and there we just make, a loop and can start from first message on topic :slight_smile:
I need a way to simple clean entity from many components that was added to in on it lifetime to become clear archetype like it was on creation.

I think not EntityId nor EntityGUID is not for network sync. it just data that we can use or can ignore.
My logic is that we already have unique entity ids and create additional layer and make sure that additional layer in sync with primary one on every machine on network is just waste of time to create fix all bugs and support, waste of memory and game performance, waste of code clarity…

Using prespawned pool of Entities (Manual id manipulation) is great thing.

on id range 0-19 - Entities of Players
on id range 20-39 - Entities of PlayerMobs

this ids lower than 255 so I need to send only one byte over network for it.
They always in step 20 from each other so Having Entity of Player i can just add 20 and get entity of it Mob and fast chech that player have no mob for time.

Adding those ids into Entity names is great helper for debugging because it very easy to to find mob of Player is it exists…
And they all the same on all clients.

So many Pros and for now just one Con: We have no Api to quick move entity to exact archetype.

You could have reliable syncing, storing general information in some form of array (NativeArray, BufferArray, hash, etc), instead of entities.
With entities, you need deal also with their version at some point.

This is exactly what I call waste of everything, is not performance by default this is slowness by default and messy code by default.

About Version you not right it always will be 1 so this is legitimate code

var entity = new Entity{ Index = netData.Index, Version = 1 };

just because Entity is stable reference and while I not destroy it, Index and version stay unchanged.

Not really. Since you use specifically same data set and as you said, you don’t need filtering, accessing NativeArray is likely much faster than entities.
Adding components outside archetype, is where things can get messy really.
But since you doing it once, is not that important I suppose?

What I meant regarding version, as indeed I assumed your version will be one, that you need set that value, to get full entity reference, every time you access it. However, here is small trap you may fall into, if not being careful.

Imagine you, or someone add part of the ECS code in the future, just before your polling entities are created. And this code creates and destroyed some entities. Then your “spare” entity, will have increased version. Therefore, it won’t be 1 anymore.

You would have to absolutely guarantee, that no one ever add new system, which manipulates entities, before you generate n required entities? Is hard to remember such quirks year later for example :slight_smile:

I asked while ago, if entity can be accessed without version, as this would suite me as well. But apparently there is not an option at current state. Unless something has changed.