SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Sleak::Component Class Referenceabstract

#include <Component.hpp>

Inheritance diagram for Sleak::Component:

Public Member Functions

 Component (GameObject *object)
virtual ~Component ()=default
virtual void FixedUpdate (float fixedDeltaTime)
GameObjectGetOwner ()
virtual bool Initialize ()=0
 One-time setup, called after the component is attached and the owner is initialized.
virtual void LateUpdate (float deltaTime)
virtual void OnDestroy ()
 Called when the component is detached or the owner is destroyed.
virtual void OnDisable ()
virtual void OnEnable ()
virtual void Update (float deltaTime)=0
Public Member Functions inherited from Sleak::Object
 Object (const std::string &name="Object")
virtual ~Object ()=default
const std::string & GetName () const
uint64_t GetUniqueID () const
void SetName (const std::string &value)

Protected Attributes

bool bIsInitialized
GameObjectowner

Detailed Description

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.

class SpinComponent : public Sleak::Component {
public:
SpinComponent(Sleak::GameObject* owner, float degreesPerSecond)
: Sleak::Component(owner), m_speed(degreesPerSecond) {}
bool Initialize() override {
m_transform = GetOwner()
return m_transform != nullptr;
}
void Update(float deltaTime) override {
if (!m_transform) return;
m_transform->RotateAround(
Sleak::Math::Vector3D(0.0f, 1.0f, 0.0f),
m_speed * deltaTime);
}
private:
float m_speed;
Sleak::TransformComponent* m_transform = nullptr;
};
// Attach it; 90.0f is forwarded to the constructor
obj->AddComponent<SpinComponent>(90.0f);
GameObject * owner
Definition Component.hpp:81
Component(GameObject *object)
Definition Component.hpp:61
virtual bool Initialize()=0
One-time setup, called after the component is attached and the owner is initialized.
GameObject * GetOwner()
Definition Component.hpp:76
virtual void Update(float deltaTime)=0
T * GetComponent()
Finds the first attached component of type T, or nullptr.
Represents the position, rotation, and scale of an entity in 3D space.
void RotateAround(const Vector3D &axis, float angle)
Rotates by angle around axis in world space.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
See also
GameObject, TransformComponent, CameraController

Definition at line 59 of file Component.hpp.

Constructor & Destructor Documentation

◆ Component()

Sleak::Component::Component ( GameObject * object)
inline

Definition at line 61 of file Component.hpp.

◆ ~Component()

virtual Sleak::Component::~Component ( )
virtualdefault

Member Function Documentation

◆ FixedUpdate()

virtual void Sleak::Component::FixedUpdate ( float fixedDeltaTime)
inlinevirtual

Definition at line 68 of file Component.hpp.

◆ GetOwner()

GameObject * Sleak::Component::GetOwner ( )
inline

Definition at line 76 of file Component.hpp.

◆ Initialize()

virtual bool Sleak::Component::Initialize ( )
pure virtual

◆ LateUpdate()

virtual void Sleak::Component::LateUpdate ( float deltaTime)
inlinevirtual

Definition at line 69 of file Component.hpp.

◆ OnDestroy()

virtual void Sleak::Component::OnDestroy ( )
inlinevirtual

Called when the component is detached or the owner is destroyed.

Definition at line 72 of file Component.hpp.

◆ OnDisable()

virtual void Sleak::Component::OnDisable ( )
inlinevirtual

Reimplemented in Sleak::MaterialComponent.

Definition at line 74 of file Component.hpp.

◆ OnEnable()

virtual void Sleak::Component::OnEnable ( )
inlinevirtual

Reimplemented in Sleak::MaterialComponent.

Definition at line 73 of file Component.hpp.

◆ Update()

Member Data Documentation

◆ bIsInitialized

bool Sleak::Component::bIsInitialized
protected

Definition at line 82 of file Component.hpp.

◆ owner

GameObject* Sleak::Component::owner
protected

Definition at line 81 of file Component.hpp.