deep clone entity components
See original GitHub issueContextExtensions.CloneEntity creates entity with shallow copied components
public static TEntity CloneEntity<TEntity>(this IContext<TEntity> context, IEntity entity, bool replaceExisting = false, params int[] indices) where TEntity : class, IEntity
{
TEntity entity1 = context.CreateEntity();
entity.CopyTo((IEntity) entity1, replaceExisting, indices);
return entity1;
}
I’m using prototype entities, which are cloned at runtime. So far I stored behaviour classes with readonly data inside component fields, so shallow copying kinda worked. But recently added BehaviourTreeComponent for AI and it needs to store some data.
What could be a solution to get entity deep clone? Or can I get out with storing POD components?
Issue Analytics
- State:
- Created 6 years ago
- Comments:15 (2 by maintainers)
Top Results From Across the Web
Entity Framework 5 deep copy/clone of an entity
One cheap easy way of cloning an entity is to do something like this: var originalEntity = Context.MySet.AsNoTracking() .
Read more >deep clone entity components · Issue #457 · sschmid/Entitas
I'm using prototype entities, which are cloned at runtime. So far I stored behaviour classes with readonly data inside component fields, ...
Read more >Deep Cloning Objects in JavaScript, the Modern Way
It's been a long time coming, but we finally now have the built-in `structuredClone` function to make deep cloning objects in JavaScript a ......
Read more >Deep-copying in JavaScript using structuredClone - web.dev
One way to create a shallow copy in JavaScript using the object ... Back then, JSON.parse() was the fastest option for very small...
Read more >Methods for deep cloning objects in JavaScript
There are several ways to shallow clone objects in JavaScript, but deep cloning objects is trickier. We highlight several methods to do so....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi, 2 thoughts, one on deep copy and one on an alternative solution for your problem:
Deep Copy A generic solution for deep copying is probably not a good idea. Deep cloning a component with complex objects is probably way more complicated and more overhead compared to simply construct new objects manually. Deep copying can make sense if you know what objects you are dealing with, but I wouldn’t strive for a general solution. Objects might have references to other objects that have to be cloned that have references to objects that have to be cloned that have other ref… and so on, which leads me to:
Configuration / Entity Factory If I understand correctly you basically trying to create some template / blueprint entities that you will clone at runtime. If you break it down, this is basically configuration. I worked on many games with lots of external configuration. Configs are simple first and can grow in complexity. Chances are that game designers and level designer will take over the responsibility to maintain configurations. I recommend having tools for working with configs like google sheets, custom Unity inspectors or sth like that which output a versioned config in structure independent from your game architecture, e.g. Text formats like JSON or binary formats like Flax Buffers / protobuf or similar.
We created a class, e.g. CharacterSetupService, BuildingSetupService that know how to construct entities based on the configuration, e.g.
This approach also allows you to simply write unit tests because you can mock the CharacterSetupService and have config independent tests
I see too many cases where deep copying doesn’t make sense and therefore can’t be included in Entitas as a general solution. If you have specific cases where you need it, you have to implement it yourself, which I think would be the better way anyway as you have oversight on what objects you actually plan to deep copy.