Skip to content

Instantly share code, notes, and snippets.

@danielytics
Last active April 26, 2022 11:49
Show Gist options
  • Save danielytics/dd2cd6601c85f3b210e4bc0541febf58 to your computer and use it in GitHub Desktop.
Save danielytics/dd2cd6601c85f3b210e4bc0541febf58 to your computer and use it in GitHub Desktop.
void mergeEntity (entt::registry& source_registry, entt::registry& destination_registry, entt::entity source_entity, entt::entity destination_entity, bool overwrite_components)
{
for(auto [id, source_storage]: source_registry.storage()) {
auto destination_storage = destination_registry.storage(id);
if (destination_storage != nullptr && source_storage.contains(source_entity)) {
if (! destination_storage->contains(destination_entity)) {
destination_storage->emplace(destination_entity, source_storage.get(source_entity));
// If destination already contains the component, then either skip or "overwrite"
} else if (overwrite_components) {
destination_storage->erase(destination_entity);
destination_storage->emplace(destination_entity, source_storage.get(source_entity));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment