Best way to copy a subset of components from one entity to another? #51
Replies: 1 comment 7 replies
-
Hey there ! :) Looks like you just found a missing feature. That was not on my screen at all. There is a way to copy components (the chunks do this internally), but the way is a bit more manual work. If have access to the internalsFor now, you can try just that. You need to get the chunk of the two entities. From this chunk you get the component arrays and then you just have to copy the component manually. You must also make sure that both entities have the components to be copied. For example, if you copy position and velocity from one entity to the other, both entities will need these components. If they don't exist on the entity yet, logically you have to add them first :) var entityInfo = world.EntityInfo[entity.Id];
var otherEntityInfo = world.EntityInfo[otherEntity.Id];
var chunk = entity.GetChunk();
var otherChunk = otherEntity.GetChunk();
var firstArray = chunk.GetArray<...>();
var secondArray = otherChunk .GetArray<...>();
// Copy from item from first array into second array at the same position, entityInfo contains the slot of an entity inside its chunk.
// Repeat for all components which should be copied.
// Theres also chunk.GetArray(type) in case you cant use generics If you use the nugetThen we have a problem... in this case you can't access the internals and since the copy feature doesn't exist yet you will have to use generics first. var toCopy = entity.Get<Component>();
if(otherEntity.Has<Component>()) otherEntity.Set(toCopy);
else otherEntity.Add(toCopy);
// repeat for other components Im gonna work quickly on this to provide a clean copy mechanism :) |
Beta Was this translation helpful? Give feedback.
-
So, I'm trying to copy some components from one entity to another entity, where the component structs are tagged with some attribute.
For context, I'm trying to copy components specific to rendering by tagging specific components with a
[ImportantToRendering]
attribute. The idea is that I want to snapshot the state of the world at the end of a fixed simulation tick so that the renderer can interpolate from the previous state to the current state for variable rendering speed.I am able to grab, for a given entity, all of the components that should be mirrored over to the render state. However, actually copying the ImportantToRendering tagged components on the entity that should be copied is eluding me.
Any advice here on how to bulk copy some components from one entity to another?
Beta Was this translation helpful? Give feedback.
All reactions