Hello Everyone,
I am new to DOTS and trying to understand the examples.
I have recently tried to refactor Hello_Cube_07_Fluent_Query’s two systems, the “spawner” script (HelloSpawnMonoBehaviour) and also the “MovementSystem” script in order to use IJobChunk.
When I just refactor the MovementSystem, it works fine even with Burst, but I couldn’t figure out how to refactor to implement IJobChunk in the “spawner” script. I mostly get I cannot use Burst for spawning job.
MovementSystem and SpawnerSystem codes are below. If you can redirect me to the correct implementation, I would be glad.
Thanks in advance.
MovementSystem.cs
using UnityEngine;
using Unity.Entities;
using Samples.HelloCube_07;
using Unity.Collections;
using Unity.Burst;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Jobs;
[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateAfter(typeof(SpawnerSystem))]
public class JobMovementSystem : JobComponentSystem
{
private EntityQuery selectionGroup;
private EndInitializationEntityCommandBufferSystem entityCommandBufferSystem;
protected override void OnCreate()
{
EntityManager entityManager = World.Active.EntityManager;
selectionGroup = GetEntityQuery(typeof(MoveUp), typeof(MovingCube), typeof(Translation));
entityCommandBufferSystem = World.GetOrCreateSystem<EndInitializationEntityCommandBufferSystem>();
}
[BurstCompile]
private struct MovementJob : IJobChunk
{
[ReadOnly]public float DeltaTime;
public ArchetypeChunkComponentType<Translation> positionType;
[ReadOnly] public EntityCommandBuffer commandBuffer;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
{
var positionChunks = chunk.GetNativeArray(positionType);
for (var i = 0; i < chunk.Count; i++)
{
var position = positionChunks[i];
if (position.Value.y > 8f)
{
position = new Translation
{
Value = new float3(position.Value.x, -8f, position.Value.z)
};
}
position = new Translation
{
Value = new float3(position.Value.x, position.Value.y + DeltaTime, position.Value.z)
};
positionChunks[i] = position;
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var positionType = GetArchetypeChunkComponentType<Translation>(false);
var job = new MovementJob
{
positionType = positionType,
DeltaTime = Time.deltaTime,
commandBuffer = entityCommandBufferSystem.CreateCommandBuffer()
};
return job.Schedule(selectionGroup, inputDeps);
}
}
SpawnerSystem.cs
```csharp
**using UnityEngine;
using Unity.Entities;
using Samples.HelloCube_07;
using Unity.Collections;
using Unity.Burst;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Jobs;
[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateAfter(typeof(SpawnerSystem))]
public class JobMovementSystem : JobComponentSystem
{
private EntityQuery selectionGroup;
protected override void OnCreate()
{
EntityManager entityManager = World.Active.EntityManager;
selectionGroup = GetEntityQuery(typeof(MoveUp), typeof(MovingCube), typeof(Translation));
}
[BurstCompile]
private struct MovementJob : IJobChunk
{
[ReadOnly]public float DeltaTime;
public ArchetypeChunkComponentType<Translation> positionType;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
{
var positionChunks = chunk.GetNativeArray(positionType);
for (var i = 0; i < chunk.Count; i++)
{
var position = positionChunks[i];
if (position.Value.y > 8f)
{
position = new Translation
{
Value = new float3(position.Value.x, -8f, position.Value.z)
};
}
position = new Translation
{
Value = new float3(position.Value.x, position.Value.y + DeltaTime, position.Value.z)
};
positionChunks[i] = position;
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var positionType = GetArchetypeChunkComponentType<Translation>(false);
var job = new MovementJob
{
positionType = positionType,
DeltaTime = Time.deltaTime,
};
return job.Schedule(selectionGroup, inputDeps);
}
}**
```