Hello,
I recently started to work with ECS and the job-system but I’ve encountered some issues.
when i try to move an entity array by translation nothing happens.
if some one can give me a tip why it doesn’t work I will be really grateful.
here is the piece of code:
public class SpreadCardsSystem : JobComponentSystem
{
[RequireComponentTag(typeof(MyCards))]
struct SendCardsToPosJob : IJobForEachWithEntity<Rotation, MyCards, Translation>
{
[DeallocateOnJobCompletion] [ReadOnly] public NativeArray<CardWithPos> eCards;
public EntityCommandBuffer.Concurrent entityCommandBuffer;
public void Execute(Entity entity, int index, ref Rotation rotation, ref MyCards myCards, ref Translation translation)
{
translation.Value = eCards[index].position;
rotation.Value.value.z = 90f;
entityCommandBuffer.SetComponent(index, eCards[index].entity, translation);
}
}
private EndSimulationEntityCommandBufferSystem endSimulationEntityCommandBufferSystem;
protected override void OnCreate()
{
endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
base.OnCreate();
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
JobHandle jobHandle = new JobHandle();
try
{
EntityQuery entityQuery = GetEntityQuery(typeof(MyCards), ComponentType.ReadOnly<Translation>());
NativeArray<Entity> arr = entityQuery.ToEntityArray(Allocator.TempJob);
if (arr.Length == 0)
{
arr.Dispose();
return jobHandle;
}
else
Debug.Log(arr.Length.ToString());
NativeArray<CardWithPos> enemyCardWithPosArr = new NativeArray<CardWithPos>(arr.Length, Allocator.TempJob);
for (int i = 0; i < arr.Length; i++)
{
enemyCardWithPosArr = new CardWithPos
{
entity = arr,
position = new float3(4f + (-2 * i),-3f,0)
};
}
arr.Dispose();
SendCardsToPosJob sendCardsJob = new SendCardsToPosJob
{
eCards = enemyCardWithPosArr
};
enemyCardWithPosArr.Dispose();
jobHandle = sendCardsJob.Schedule(this, inputDeps);
endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);
}
catch (System.Exception)
{
}
return jobHandle;
}
}
Remove Try Catch, it’s hiding your real problems especially when you use empty catch. Relly bad practice.
You enemyCardWithPosArr.Dispose(); after job declaration and you job will throw error that native array was disposed but you try to use it. You have DeallocateOnJobCompletion, and you don’t need use enemyCardWithPosArr.Dispose(); in that case.
enemyCardWithPosArr = new CardWithPos
{
entity = arr,
position = new float3(4f + (-2 * i),-3f,0)
};
You assigning whole array instead of one entity here
I removed the try and catch and got the following error for some reason:
EntityQueryDescValidationException: EntityQuery contains a filter with duplicate component type name MyCards. Queries can only contain a single component of a given type in a filter.
When i remove the enemyCardWithPosArr.Dispose() it says it isn’t getting disposed.
As for the enemyCardWithPosArr its because i want to update a native array with different position to each entity.
Nevertheless non of it works because the translation won’t update.
If there is anything else i should do differently to make to job work please let me know.
Error says clearly - you have duplicates. You using MyCards in IJFWE and at the same time [RequireComponentTag(typeof(MyCards))]
And you EntityQuery should be in OnCreate! Your code is real mess…you using ReadOnly for Translation for MyCards and at the same time you using IJFWE with writable Translation. You have query and not use it in IJFWE.
Don’t worry, @InAngel , your code isn’t “a mess”. It just has errors right now because you’re just learning the DOTS api. That’s reasonable, right? A few folks on here like to answer questions, but they don’t like thinking about people. Just check out the examples and documentation, and these types of errors will be a memory soon.
The API isn’t always intuitive right now, since it’s still in very active (and changing) development. It’s been getting more intuitive over time.
Start from samples, “understand” them, cause in your case is core misunderstandings. After that “grow strength” and add new things, but not before you really understand how things work and whi it is what it is.
Sugar not will help. Straight truth always better. Without painful truth learning process much slower and ineffective. Moreover I pointed to his code problems.
You are right i have much to learn in the subject.
do you happen to know why I am getting these warnings:
Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
even with a very simple jobsystem
public class MoveCardsSystem : JobComponentSystem
{
BeginInitializationEntityCommandBufferSystem m_EntityCommandBufferSystem;
protected override void OnCreate()
{
// Cache the BeginInitializationEntityCommandBufferSystem in a field, so we don't have to create it every frame
m_EntityCommandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
}
struct MoveJob : IJobForEachWithEntity<MyCards, Translation>
{
public EntityCommandBuffer.Concurrent CommandBuffer;
public void Execute(Entity entity, int index, [ReadOnly] ref MyCards myCards, [ReadOnly] ref Translation translation)
{
var position = new float3(4f + (index * -2), -3f, 0f);
CommandBuffer.SetComponent(index,entity, new Translation { Value = position });
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var job = new MoveJob
{
CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent()
}.Schedule(this, inputDeps);
m_EntityCommandBufferSystem.AddJobHandleForProducer(job);
return job;
}
}
Look. No offence, but @eizenhorn probably knows things better than most of us in this field
In the end, his thread is sticky practical example ** Unity ECS and Job System in production **
He rose valid points in most constructive way and with examples, which allows OP to learn from, for which OP has confirmed.
No need for references, which not everyone understand.
Not better than other old guys on this forum branch It was just misunderstanding, probably cause of english not my native language, and my russian way of thinking and constructing sentenses… not always can be reinterpreted by other people right, we solved it in PM. Not need in other discussion here, it’ll get a bit offtop I think Peace, friendship, bubble gum. @hippocoder if you want, cleanup some things here
My apologies. I sometimes see things that aren’t really there. I feel awful that other people were affected. I’m sorry to have diverted your thread, @InAngel .