Entt: How to clone registry?

Created on 4 Nov 2020  路  13Comments  路  Source: skypjack/entt

Hello every one

I want to basically duplicate a registry and maybe run some physics simulation so the main registry is not modified.
Is it possible with snapshots? + can someone explain what a snapshot really is?

thanks for the awesome library btw

question

Most helpful comment

Hi, thank you for using EnTT!

A registry isn't meant for copies, because it may be very expensive.
Though, you can copy entities and components between registries easily:

// all entities
dst.assign(src.data(), src.data() + src.size(), src.destroyed());

// everything of a given type
auto view = registry.view<T>();
dst.insert(view.data(), view.data() + view.size(), view.raw(), view.raw() + view.size());

A snapshot is the result of a paid work 馃槃 and tbh I wouldn't use it nowadays, since range-functionalities are way faster.

All 13 comments

Hi, thank you for using EnTT!

A registry isn't meant for copies, because it may be very expensive.
Though, you can copy entities and components between registries easily:

// all entities
dst.assign(src.data(), src.data() + src.size(), src.destroyed());

// everything of a given type
auto view = registry.view<T>();
dst.insert(view.data(), view.data() + view.size(), view.raw(), view.raw() + view.size());

A snapshot is the result of a paid work 馃槃 and tbh I wouldn't use it nowadays, since range-functionalities are way faster.

what that src.destroyed() means?

It returns the _head_ of the list of destroyed entities.
This is how EnTT manages its entities: https://skypjack.github.io/2019-05-06-ecs-baf-part-3/
So, destroyed literally returns an integral value that contains enough information to know _where_ this list is. In a sense, this value is the full list.

I am a student and right now I don't have much time reading that and I am going to close this issue.
I will definitely read that link and play around with this solution you gave me later.
Thank you very much 馃榾.

Ahah no worries. It was only for reference.
If you have any other question, I invite you to join the gitter channel or the discord server. There are many people there who can help. :+1:

I am using an older version of EnTT this is the entt.hpp used in my engine and registries, don't have any public destroyed method. Is it a new feature? when I tried to update EnTT, I got a lot of weird errors.

Ah, interesting. I've just realized that the single include file doesn't contain the version!
By the way, destroyed is pretty recent, yeah. Previous versions work without it:

// all entities
dst.assign(src.data(), src.data() + src.size());

Long story short, it's a matter of performance. With the value returned by destroyed, assign performs only a raw copy of the vector of entities. Previous versions iterated the vector to reconstruct the implicit list of destroyed entities instead.

Ok. I don't care about performance in this situation. Thanks 馃檹馃徎.

You're welcome. 馃檪

I made a copy-constructor for my Scene class like so:

Scene::Scene(const Scene& other)
    : m_Registry(), m_ViewportWidth(other.m_ViewportWidth), m_ViewportHeight(other.m_ViewportHeight),
      m_RootHandles(other.m_RootHandles), m_ComponentList(other.m_ComponentList)
{
    const auto& reg = other.m_Registry;
    m_Registry.assign(reg.data(), reg.data() + reg.size());
}

looks like entities are copied fine but there is no component attached! (or at least my TransformComponent which is attached to all entities)

and one more thing:
all my components, store a Scene* and an entt::entity and when they are copied, I want to change that Scene* to point to the new scene. Is there any better way to do that than looping through all components after copy?

assign copies entities between registries, it doesn't copy their components. 馃檪
Long story short, most of the time I don't want to do a blind copy of all types, because it doesn't make muche sense. For example, I use components to dispatch messages and they shouldn't be copied in any case.
Take a look at insert to also copy components of interest if needed. 馃憤

Also, can I suggest you to join the gitter channel or the discord server for your questions?
I'm fine with using GH, but it may be somewhat better with a chat?

OK, It makes more sense to copy different component types one by one. I can also apply my modifications to them.
+I personally prefer GH but I will give gitter and discord a try.

+I personally prefer GH but I will give gitter and discord a try.

Oh, no, don't worry, I'm fine with GH. I suggested it only in case you're interested in a more direct chat. 馃憤

Was this page helpful?
0 / 5 - 0 ratings