Entitas.Context.DestroyEntity C# (CSharp) Method

DestroyEntity() public method

public DestroyEntity ( Entity entity ) : void
entity Entity
return void
        public virtual void DestroyEntity(Entity entity)
        {
            var removed = _entities.Remove(entity);
            if(!removed) {
                throw new ContextDoesNotContainEntityException(
                    "'" + this + "' cannot destroy " + entity + "!",
                    "Did you call context.DestroyEntity() on a wrong context?"
                );
            }
            _entitiesCache = null;

            if(OnEntityWillBeDestroyed != null) {
                OnEntityWillBeDestroyed(this, entity);
            }

            entity.destroy();

            if(OnEntityDestroyed != null) {
                OnEntityDestroyed(this, entity);
            }

            if(entity.retainCount == 1) {
                // Can be released immediately without
                // adding to _retainedEntities
                entity.OnEntityReleased -= _cachedEntityReleased;
                _reusableEntities.Push(entity);
                entity.Release(this);
                entity.removeAllOnEntityReleasedHandlers();
            } else {
                _retainedEntities.Add(entity);
                entity.Release(this);
            }
        }

Usage Example

 void createTestEntityError(Context context)
 {
     context.DestroyEntity(context.CreateEntity().Retain(this));
 }