Skip to content

ECS Framework API v2.2.13


ECS Framework API / tryGetComponent

Function: tryGetComponent()

tryGetComponent<T>(entity, componentType): undefined | ComponentInstance<T>

Defined in: packages/core/src/ECS/TypedEntity.ts:54

尝试获取组件

Type Parameters

T

T extends ComponentConstructor

Parameters

entity

Entity

实体实例

componentType

T

组件类型构造函数

Returns

undefined | ComponentInstance<T>

组件实例或undefined

Example

typescript
const health = tryGetComponent(entity, Health);
if (health) {
    health.value -= 10;
}

Released under the MIT License.