If you using the monobehavior technique to create your entities (ComponentDataWrapper) instead of directly creating entities, I cannot figure out how to initialize the entity. I tried overriding awake on the ComponentDataWrapper but that broke things. I can set simple things right in the editor, but i want to make a Unity.Mathematics.Random which needs to be initialized with a seed.
Systems seem to have 2 ways to run(correct me here if i’m wrong). One technique is unity main thread safe
eizenhorn is correct you would use a file that loads into the scene where you create all your entities. Something like:
var entityManager = World.Active.GetOrCreateManager<EntityManager>();
for (int i = 0; i < 3; i++)
{
Entity playerUnit = entityManager.CreateEntity(PlayerUnitArchetype);
entityManager.SetComponentData(playerUnit, new Position { Value = new float3(i * 5, 0.5f, 0) });
entityManager.AddSharedComponentData(playerUnit, cubeRenderer);
}
This might be a little incorrect. ComponentSystems are indeed called from the main thread but jobs leverages multi-thread through the use of workers: Unity - Manual: Job system overview
To answer OP, yes you should definitely use jobs when you can and regular systems when you can’t. Unity just came out with a new sample that shows what you might be trying to achieve:
In the example they grab the HelloSpawner component (which stores the prefabs) and instantiate through the EntityManager. So if you had 5 spawners in the scene with 5 different prefabs, it’ll go iterate through all of them and spawn your entities.
The system itself is still always executed on the main thread.
And there is nothing stopping you creating jobs in a regular ComponentSystem that will run in separate threads (and you don’t even need to finish them before the system returns though this idea is extremely unwise.)
Thanks for the input. However the first question was not answered as I was hoping. The suggestions were do not use the unity based entity creation. But that is what I am trying to do(ComponentDataWrapper). Using constructor is not possible because unity is creating the entity for me. That is why I cannot figure out how to customize it.
This is because it seems like you no longer need the bootstrapping file if you want to create all your entities via game objects to move more towards traditional scene design.
you define an “InitializeRandom” component and attach to your GOE
you write a system that takes the “InitializeRandom” and your rng holder components, initializes the rng from some global entropy pool (e.g. a single System.Random or UnityEngine.Random) and removes the Init component
eg:
struct Rng : IComponentData {public Unity.Mathematics.Random value;}
struct InitializeRng : IComponentData {} // empty
class RngComponent : ComponentDataWrapper<InitializeRng>{}
class RandomInitSystem : ComponentSystem {
System.Random globalRandom;
OnCreateManager() {globalRandom = new Random();}
OnUpdate() {
ForEach((Entity e, ref InitializeRng _) {
RemoveComponent<InitializeRng>(e);
AddComponent(e, new Rng{value = new Random(globalRandom.Next()});
});
}
}
Im having trouble parsing the foreach loop in your on update. All the examples I have seen so far show a syntax to my original post. You make a component group and then in your update you use something like