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

Question How can I call GetEntity(go) outside of the baker?

Discussion in 'Entity Component System' started by Dog_Like, Jun 7, 2023.

  1. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    I'm currently trying to create a custom Entity World, so I have disabled the default world by adding a script define. I have encountered an issue where, in the default world, I can use the baker to directly convert prefabs into entities.

    However, in the custom world, this doesn't seem to work, and the bake process doesn't run. Is there a way to use the baker in a custom world, especially for prefabs?

    Or is there any way to convert a prefab into an entity?
     
  2. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    943
    What you can do is collect all your prefabs under a subscene and use GetEntity() to convert these into "entity prefabs". You then maintain these entities in some kind of hashmap stored in a component or in blob asset. You can then have access to these prefabs at runtime and instantiate them either by EntityManager or EntityCommandBuffer.
     
  3. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    Thank you for your response. When disabling the default world, it seems that the baker does not execute.
     
  4. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    239

    Be aware that you can move entities, like prefabs, between worlds with entityManager.MoveEntitiesFrom().

    I don't know what is your use case here but let me advise against creating more worlds. This was my initial idea as well but it only created more problems and made me discover "surprises" like this one here (in my case I learned that rendering can happen in a single world only).
     
    Last edited: Jun 8, 2023
  5. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25

    Thank you! I just want to customize which systems run in my custom world and manually control the update. So I manually create the world and add systems to it. However, it seems that I don't get any help from the baker and encounter many issues instead. I will try using DefaultWorldInitialization.Initialize() to reset the world later. Do you have any suggestions for disabling the default world's update and manually calling it?
     
  6. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    239
    The best resource for getting to know how to setup more complex worlds is to start reading ClientServerBootstrap.cs (an ICustomBootstrap) from netcode package. Everything relevant is either there or in referenced classes/files.
     
  7. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    Thank you for your kind assistance. I will try using ICustomBootstrap later. For now, I have decided to give up on custom systems and use "require" to enable and disable systems. After creating the default world, I disable updates and manually call update.

    This essentially solves my problem, except for the inability to load and release entity prefabs when needed.