Can we check if entity exists from a parallel job?

I see that the EntityManager.Exists method is the most optimal way of checking if an entity exists. So currently I gather data I need on the main thread using this method. However it would be tidier if I could do this from parallel jobs. The only way I have done this is to pass in the array of entities from a query and use ‘Exists’ method on the array, however I understand that is an iterative method so not good if I end up dealing with thousands of entities, whereas the EntityManager.Exists method accesses using index to is direct.

In the code for EntityManager.Exist it is commented:

// @TODO Point to documentation for multithreaded way to check Entity validity.

But I can’t find any information on that. So does anyone know a non-iterative way to do this?

You can use a ComponentDataFromEtity.HasComponent call inside a job using an entity that you know should have that component when it is alive. This call returns false for Entities that don’t have the component but also for entities that don’t exist anymore.

4 Likes

Funny, that internally calls that same internal Exists method, and then uses an iterative method over the component types in the chunk to see if it has that specific component. If that’s parallel safe then so should the EntityManager method, but we can’t pass that to parallel jobs.

Also inside SystemBase you can use HasComponent inside Entities.Foreach without any external ComponentDataFromEntity defining

3 Likes

@BrendonSmuts Cheers this worked out quite well. There is in-fact a Exists method on GetComponentDataFromEntity however that internally calls HasComponent so is basically doing the same thing. But it’s good to use to show intent.

Exists has since been renamed to HasComponent

A more specific check for entity existence can be done like this.

var entityExists = EntityManager.UniversalQuery.GetEntityQueryMask();
entityExists.Matches(entity);

The EntityQueryMasks can be passed to a parallel job.

4 Likes