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

Hello, quick and easy question

Discussion in 'Entity Component System' started by goodnewsjimdotcom, May 22, 2021.

  1. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    If I have a GameObject, how do I:

    1) Instantiate it as an Entity in Monobehavior assuming I already have an Entitymanager reference and a Gameobject?

    2) Instantiate it in ECS Jobs?

    Thank you,
    The old stuff I found from Code Monkey no longer worked: https://hatebin.com/nvtorrktfh
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    I suggest to look into game object to entity conversion work flows.
     
  3. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    Ok, after reading: https://docs.unity3d.com/Packages/c...html#the-iconvertgameobjecttoentity-interface

    I got this error on my attempt at understanding the tech: https://hatebin.com/trzkpmmoeo

    I always look at programming like being able to tell time. It is easy, anyone trained can do it. But go ahead and try to figure out to set a clock on your new Toyota. You can't understand what the engineers were thinking. So you need to find someone to help you set the clock then you're good to go, you can tell time again. This is what techs are like.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    Please use code tag, when referencing the code, rather than external links.
    It is important, that when using code, you got matching packages and Unity version. Otherwise you will most likely experience the errors.

    From your link above, try as an example
    IConvertGameObjectToEntity interface
    Then follow into next headline
    Generated authoring components
    Put created MB on GO. Assign prefabs to script fields (it is important to be prefab).
    Assign on both prefabs and GO with script, Convert to Entity components.

    And you should be good to go.
     
  5. AaronMahler

    AaronMahler

    Joined:
    May 19, 2021
    Posts:
    8
    Sound looks interesting. Are you going to develop a new game?
     
  6. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    Actually that code someone gave me is answering a question I did not ask, of how to link entities after creation, but I already have a system for that.

    I simply want to take a GameObject or prefab GameObject, and have it become an Entity in a Monobehavior script. This should not be difficult for one of you guys out there. It is probably one line of code.


    -------------------

    Reiterate from my Discord discussion:
    I think I'm asking the wrong question... People are telling me how to access an entity after converted from GameObject. I want to know how to convert a GameObject to an Entity, not collect Entity data after that happened.

    I don't even care if I get an entity reference, I can do that elsewhere.

    I just want to take a GameObject and put it into the world exactly as is as an entity.

    Via Monobehavior for now


    --------

    Day Three on this...
     
    AaronMahler likes this.
  7. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    Can you not just add a ConvertToEntity component to the GameObject?
     
  8. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    I eventually tried that just before getting this, and that did work. I didn't know that was how you did it, my coworker said something that alluded me to that. Thank you. You had the correct answer. Now I need to figure out how to set the conversion mode to convert and inject.

    GameBoardModel.peers[a].myShipGameObject.AddComponent<ConvertToEntity>(); //This converts and destroys an entity. How do I set the conversionMode?
    [9:32 PM]
    If you add a ConvertToEntity component on a gameObject, it solves the problem of turning a gameObject into an Entity.
    [9:32 PM]
    I need convert and inject however.
     
  9. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    OOOH! I can just search this forum for keywords, and boom, sample code everywhere sorted by date! WOOO! Ok, now ECS/DOTS, you're about be crushinated!

    My code I haven't tested but I assume works:

    GameBoardModel.peers[a].myShipGameObject.GetComponent<ConvertToEntity>().ConversionMode = ConvertToEntity.Mode.ConvertAndInjectGameObject;

    Thanks guys for your patience. I'm about to be self sufficient for the most part now.
     
  10. AaronMahler

    AaronMahler

    Joined:
    May 19, 2021
    Posts:
    8
    That's a very good idea if you can implement this game object. Carry on!