Base for behavior attached to a GameObject. Owner deletes it via GameObject::RemoveComponent(), never directly.
Derive from Component to add your own behavior to an object. The constructor must take the owning GameObject* as its first parameter, because GameObject::AddComponent() supplies it and forwards the rest. Initialize() and Update() are pure virtual; FixedUpdate(), LateUpdate(), OnEnable(), OnDisable(), and OnDestroy() are optional.
The owning GameObject holds components in a RefPtr and destroys them with itself, so never delete a component directly. Reach the owner through GetOwner() to find sibling components.
Lifecycle: the object calls Initialize() once (immediately if the object is already initialized when the component is attached, and otherwise during the object's own Initialize()), then OnEnable() if the object is active, then Update() every frame.
public:
return m_transform != nullptr;
}
void Update(
float deltaTime)
override {
if (!m_transform) return;
m_speed * deltaTime);
}
private:
float m_speed;
Sleak::TransformComponent* m_transform = nullptr;
};
obj->AddComponent<SpinComponent>(90.0f);
Component(GameObject *object)
virtual bool Initialize()=0
One-time setup, called after the component is attached and the owner is initialized.
virtual void Update(float deltaTime)=0
T * GetComponent()
Finds the first attached component of type T, or nullptr.
Root namespace for everything the engine exposes.
- See also
- GameObject, TransformComponent, CameraController
Definition at line 59 of file Component.hpp.