What’s the right approach to authoring a singleton entity?
Here’s what I’m trying to accomplish.
I have a component for storing the necessary data for a 2d grid.
[GenerateAuthoringComponent]
public struct GridSize : IComponentData
{
public int width;
public int height;
public float cellSize;
public float3 gridOrigin;
}
I want to make this entity into a singleton so I can access the data in systems that need it to translate between grid space and world space.
So… I think I might have answered my own question… but the answer seems crazy to me.
It looks like you don’t have to do anything to make this a singleton besides only having one existing entity created this way. I was going crazy trying to find the right place to call SetSingleton.
However this system just works.
public class FixedGridLocationSystem : SystemBase
{
private EndSimulationEntityCommandBufferSystem _endSimulationEntityCommandBufferSystem;
protected override void OnCreate()
{
base.OnCreate();
_endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var gridSizeEntity = GetSingletonEntity<GridSize>();
var gridSize =
World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<GridSize>(gridSizeEntity);
var ecb = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
Entities.WithAll<Tag_FixedGridPosition>().ForEach(
(Entity e, int entityInQueryIndex, ref Translation translation, in GridLocation gridLocation) =>
{
translation.Value = GridMappingHelper.GridCenterLocationFloat3(gridLocation.x, gridLocation.y,
gridLocation.z, gridSize.cellSize, gridSize.gridOrigin);
ecb.RemoveComponent<Tag_FixedGridPosition>(entityInQueryIndex, e);
}).ScheduleParallel();
_endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(this.Dependency);
}
Is this really how it’s supposed to be? You can create a singleton just by adding one Entity to a scene and then just never creating another?
Correct, Get/SetSingleton just give you errors if it doesn’t find exactly 1 entity for you, there is nothing special to create them
1 Like
That’s nice and convenient for my little test.
It seems insane in the long run. There’s no sort of protection of any kind.
Someone could create another of that entity in code or it could be part of a prefab.
I don’t think it was built to be a bullet-proof singleton system, but with this you have the tools to create one if you need some extra protection. It is just a simple mechanism to use an EntityQuery with an individual entity instead of dealing with an array of entities for just one element.
1 Like
Thanks, that’s a great way to understand it.
By these steps I don’t think bullet-proof singleton system built up, do research on Google.