Hey guys I’m working with unity dots, and I thought that These type of references work with dots?
The problem seems to be the RefRW<> Counter, when modifying the Activeentities in the IJobEntity…
Does anyone know a solution to this? what i want to achieve, is a ball-spawning mechanism that spawns entities (balls) as long as the ActiveEntities is less then the MaxEntities
I have the following code example:
[BurstCompile]
public partial struct SpawnBall_System : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
var entity = state.EntityManager.CreateEntity();
state.EntityManager.AddComponentData(entity, new EntityCounterComponent
{
ActiveEntities = 0,
MaxEntities = 2500
});
}
[BurstCompile]
public void OnDestroy(ref SystemState state)
{
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
float deltaTime = SystemAPI.Time.DeltaTime;
var ecb = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
// Schedule a job that runs the spawning logic in parallel
new BallSpawnerJob
{
DeltaTime = deltaTime,
ECB = ecb,
Counter = SystemAPI.GetSingletonRW<EntityCounterComponent>()
}.Schedule();
}
}
[BurstCompile]
public partial struct BallSpawnerJob : IJobEntity
{
public float DeltaTime;
public EntityCommandBuffer ECB;
public RefRW<EntityCounterComponent> Counter;
// This executes for each entity that has a SpawnerProperties and BallSpawnTimer.
public void Execute(BallSpawnerAspect aspect, [EntityIndexInQuery] int entityInQueryIndex)
{
if (Counter.ValueRO.ActiveEntities >= Counter.ValueRO.MaxEntities) return;
if (aspect.CanSpawnEntities() == false) { return; }
if (aspect.SpawnCooldown(DeltaTime) == false) { return; }
var entity = ECB.Instantiate(aspect.GetEntity());
ECB.AddComponent(entity, new BallTag { });
ECB.AddComponent(entity, new BallStats
{
MoneyValue = aspect.ReadProperties.ValueRO.BallValue
});
Counter.ValueRW.ActiveEntities++;
}
}
I need to keep an updated count of the entities i spawned and of entities that are active so i can limit them and stop the spawning when the limit is reached.
I’ve tried a separate method working with monobehaviours and static ints to keep the count, but then unity throws a baker-error so i changed it to the system above.
Anyone can help with this?
Thanks in advance!