Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to interact with entities from Monobehaviour?

Discussion in 'Entity Component System' started by mailfromthewilds, Apr 13, 2021.

  1. mailfromthewilds

    mailfromthewilds

    Joined:
    Jan 31, 2020
    Posts:
    215
    i have lot of cases where i need to access entity data from monobehaviour, EG get its position and spawn player there



    I see theres entitymanager.getallentities, but that will always get me an array of 40000 its very bad function IMO

    now the question is how to easily interact with some of entities? from mb
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    You can use EntityManager for these cases:
    - Store "reference" to entity that is converted / created to the Monobehaviour. Access data via that entity by using EntityManager;
    - Use a query to fetch specific entities. (E.g. tag + position + ...) - then fetch data via received entities;
    - Use singleton (GetSingletonEntity / GetSingleton) utility to fetch entity / component data.

    Queries can be created from EntityManager (just in case).
     
    Last edited: Apr 13, 2021
  3. mailfromthewilds

    mailfromthewilds

    Joined:
    Jan 31, 2020
    Posts:
    215
    oh ok do you know why GetComponentData<Child> specifically doesnt work and any way to make it work?

    i already made list of top level entities that i have on static class. but now i cant get their child
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Post the code, and errors it produces. Its hard to debug without it.
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    Because
    Child
    is
    IBufferElementData
    and you should access it differently:
    GetBuffer<Child>
     
    MNNoxMortem, Antypodish and xVergilx like this.
  6. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    How do you test if it exists?

    If you try and GetBuffer from an entity without an IBufferElementData, it errors out:

    ArgumentException: A component with type:myElement [Buffer] has not been added to the entity.
     
  7. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Code (CSharp):
    1. EntityManager.HasComponent<MyBuffer>(entity);
    works for buffers too, in fact all types afaik.
     
  8. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    Yes, thank you. Items are dropping in my game now one at a time.
     
  9. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    How can I use GetSingletonEntity from Monobehaviour?
     
  10. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    1. Get an EntityManager (e.g. from the World.DefaultGameObjectInjectionWorld);
    2. Construct EntityManager.CreateEntityQuery (can be cached somewhere for whole MonoBehaviour side if no more than one world is used);
    3. Use GetSingleton / GetSingletonEntity from EntityQuery;
     
    adammpolak likes this.
  11. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    Should
    be cached, otherwise should be disposed before next
    EntityManager.CreateEntityQuery
    , as every call (even with the same components) will create new instance and you'll see memory leak.
     
    bb8_1 and xVergilx like this.
  12. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    288