Closed PudgeKim closed 1 year ago
Currently both are impossible.
Copying an entity's components would require all components to be Clone
, I don't think that will ever be the case. With (de)serialize maybe you could do something like this but it's not implemented yet.
The easiest way you can do this now is probably a macro of all types you want to copy. Something like that:
fn copy_entity(all_storages: &mut AllStorages, entity: EntityId) -> EntityId {
let new_entity = all_storages.add_entity(());
// You can extract this macro out of the function if you pass all_storages, entity and new_entity to it
macro_rules! copy_entity_components {
($($type: ident),+ $(,)?) => {
$(
if let Some(component) = all_storages
.borrow::<View<$type>>()
.ok()
.and_then(|view| {
view.get(entity).ok().cloned()
}) {
all_storages.add_component(new_entity, component);
}
)+
};
}
copy_entity_components!(
Comp0, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8, Comp9, Comp10,
);
new_entity
}
It will go though a list of components and clone them to another entity. It'll ignore all errors, you probably can unwrap
the borrow error, the second one, only unwrap
if you copy a single kind of entity.
Vec<Box<dyn Any>>
then you could check the TypeId
to downcast. "Dynamic insert" could also exist, you could insert a component with a Box<dyn Any>
.Experimenting with serialization is on my todo list, I have a few things I want to do before that (tracking rework and workloads improvement).
Let's assume a player entitiy has many components. (let's call this entity entityP) If I want to create a new entity which will have same components entityP has, how to do it? and I also want to know all of the component's type the entityP has.
For example,