Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Wrapping frequently reoccurring Entity/Component instantiations

Discussion in 'Entity Component System' started by SomeCode, Nov 26, 2018.

  1. SomeCode

    SomeCode

    Joined:
    Jul 5, 2013
    Posts:
    32
    In several places in my code base, I have cases wherein I instantiate an Entity and Component with the intention of "doing a thing". For instance:

    Code (CSharp):
    1. CommandBuffer.CreateEntity(index)
    2. CommandBuffer.AddComponent(index, new DoThingToEntityComponent()
    3. {
    4.         TargetEntity = otherEntity
    5.         DoThingIntensity = 20.0f
    6. });
    In an OOP environment I'd absolutely wrap this up in some kind of a
    DoThingToEntity(EntityCommandBuffer.Concurrent commandBuffer, Entity targetEntity, float doThingIntensity)
    method to improve readability and reduce the potential for mistakes, and I'm tempted to do the same thing here. I was just wondering what other users might be doing to tackle this.

    The most obvious answer is to create a helper class with static methods, which is probably what I'll end up doing if I don't encounter any revelations in this thread. My next-favorite idea is to make an extension method for the relevant EntityCommandBuffer type(s), but then every helper method I create across the code base is tacked on to said type(s) and popping up in intellisense, regardless of the context in which that EntityCommandBuffer is being used.
     
    Last edited: Nov 26, 2018
    xman7c7 likes this.
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    They do this in the FPS Sample for Projectiles as well.
     
    SomeCode likes this.
  3. Deleted User

    Deleted User

    Guest

    Use CreateEntity(archetype); And SetComponent after.

    Instead of adding a component. You will save ton of perfomance.
     
  4. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    also if you need pre-configured entities you can add
    Unity.Entities.Prefab
    tag to prototype entity (this will hide it from all systems)
    and generate its clones using
    EntityManager.Instantiate
    (Prefab component is removed while cloning)
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Hmm. I hadn't actually heard of Prefab Component. Had a quick look at source and it's not super clear.
    Is there documentation or a sample around somewhere for it?
     
  6. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    It was only mentioned in release notes for 0.0.11
    https://github.com/Unity-Technologi...es/blob/master/ReleaseNotes.md#new-features-8

    also it was mentioned once by Joachim Ante before it was added :)
    https://forum.unity.com/threads/ecs-prefabs.532225/#post-3507701
     
    Last edited: Nov 26, 2018
    tertle likes this.