Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Singletons Pattern

Discussion in 'Project Tiny' started by furroy, Jun 6, 2019.

  1. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    So it feels wasteful to loop over every entity looking for what you know will be a Singleton. (Perhaps under the hood it has every entity type split out so it's looping over an array of one element which isn't really an issue?)

    I made:
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Tiny;
    3.  
    4. namespace Krunk
    5. {
    6.     public struct SingletonsComponent : IComponentData
    7.     {
    8.         [EntityWithComponents(typeof(Dir4Component))]
    9.         public Entity playerEntity;
    10.         [EntityWithComponents(typeof(MapComponent))]
    11.         public Entity mapEntity;
    12.     }
    13. }
    Added this to the Configuration, and then dragged the two entities for my player and map into these slots. (Side note: even though those were defined with EntityWithComponents, it would let me drag them into the wrong slots by accident. Seems like it should have rejected the drag if the required components weren't present?)

    Then later when I try to use them:

    Code (CSharp):
    1.             var singletons = World.TinyEnvironment().GetConfigData<SingletonsComponent>();
    2.             Entity mapEntity = singletons.mapEntity;
    3.             MapComponent map = EntityManager.GetComponentData<MapComponent>(mapEntity);
    4.             Translation lclPos = EntityManager.GetComponentData<Translation>(mapEntity);
    5.  
    It starts throwing exceptions trying to extract the components:
     
  2. robertg_unity

    robertg_unity

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    32
    Support for any type of component for the attribute
    EntityWithComponents
    is not finished, what you expected is correct thought. It will be completed in a future update.

    As for the crash, perhaps the missing entities are in a scene that hasn't been loaded yet?
     
  3. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    They are in my starting MainScreen that's set to always be loaded. I just dragged them from the hierarchy window over to the inspector window.

     
  4. robertg_unity

    robertg_unity

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    32
    Ok, I can reproduce the issue. This is a bug. This should definitively work.
     
  5. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    i don't feel like such a noob now, thanks! :)
     
  6. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    Actually I think it might be a load order issue. OnCreate and OnStartRunning both seem to be called before the entities in my main scene are live. I added logs for OnCreate and OnStartRunning.