Page 1 of 1

Check if entity exists

Posted: Mon Aug 13, 2018 6:31 am
by velocity
I need to be available to check if Entity(index) exists, because the server crashes when I try to access an entity that doesn't exist anymore.

Also, is it possible to change what an entity is available to collide with through ContentMasks?

Syntax: Select all

is_in_solid(mask=<ContentMasks.ALL: 4294967295>, generator=<class '_entities.BaseEntityGenerator'>)

Re: Check if entity exists

Posted: Mon Aug 13, 2018 3:03 pm
by L'In20Cible
You should not store Entity instances unless you use a container designed to keep them up-to-date such as EntityDictionary. To validate whether or not an instance is still valid, you would have to loop through all existing entities and compare their address which can get quite slow if you validate them often. You could also try to use a conversion function and test for an exception but as stated above, keeping instances synced by listening the creation and deletion of the entities is definitely the fastest approach.

Re: Check if entity exists

Posted: Tue Aug 14, 2018 11:55 am
by Ayuto
L'In20Cible wrote:To validate whether or not an instance is still valid, you would have to loop through all existing entities and compare their address [...]
I would like to add that this is not a recommended approach, because the entity could have been deleted and a new entity reuses the same memory address.

Like L'In20Cible metioned you should really use EntityDictionary or simply don't store the Entity instance.