question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

deep clone entity components

See original GitHub issue

ContextExtensions.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:closed
  • Created 6 years ago
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
sschmidcommented, Aug 21, 2017

Hi, 2 thoughts, one on deep copy and one on an alternative solution for your problem:

  1. 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:

  2. 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.

public virtual GameEntity CreateCharacter(Contexts contexts, string id, string type, Gender gender, int positionX, int positionY, string state) {
    var e = contexts.core.CreateEntity();

    // Add components every character needs
    SetupEntitySpecificComponents(e, id, type, gender, positionX, positionY, state);

    // Add components based on config
    AddComponentsFromConfig(contexts, e);
    return e;
}
var character = characterSetupService.CreateCharacter(contexts, id, type, gender, 0, 0, CharacterState.Idle);

This approach also allows you to simply write unit tests because you can mock the CharacterSetupService and have config independent tests

1reaction
sschmidcommented, Feb 4, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found