Entity.GetComponent<T> method

template<typename T> T& GetComponent();

Description

Returns a reference to the component of type T attached to this entity. Mutations through the reference are reflected immediately in serialization and rendering.

Important: calling GetComponent<T>() when T isn't attached triggers an EnTT assertion in Debug and is UB in Release. Use HasComponent<T>() first when the presence isn't guaranteed.

Type parameters

TcomponentAny registered component type (e.g. TransformComponent).

Returns

T&referenceMutable reference to the existing component.

Example

void OnUpdate(float ts) override {
    auto& tc = GetComponent<Thread::TransformComponent>();
    tc.Rotation.y += ts;  // spin around Y at 1 rad/s
}

See also

AddComponent · HasComponent · RemoveComponent

Entity.GetComponent<T> metoda

template<typename T> T& GetComponent();

Opis

Zwraca referencję do komponentu typu T podpiętego do tej encji. Zmiany przez referencję od razu widać w serializacji i renderowaniu.

Ważne: wywołanie GetComponent<T>(), gdy T nie jest podpięty, wywołuje asercję EnTT w Debug i jest niezdefiniowane w Release. Gdy obecność nie jest pewna, najpierw użyj HasComponent<T>().

Parametry typu

TkomponentDowolny zarejestrowany typ komponentu (np. TransformComponent).

Zwraca

T&referencjaModyfikowalna referencja do istniejącego komponentu.

Przykład

void OnUpdate(float ts) override {
    auto& tc = GetComponent<Thread::TransformComponent>();
    tc.Rotation.y += ts;  // obrót wokół Y z prędkością 1 rad/s
}

Zobacz też

AddComponent · HasComponent · RemoveComponent