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

#include <GameObject.hpp>

Inheritance diagram for Sleak::GameObject:

Public Member Functions

 GameObject (const std::string &name="GameObject")
 ~GameObject () override
void AddChild (GameObject *child)
template<typename T, typename... Args>
void AddComponent (Args &&... args)
 Constructs and attaches a component of type T. Warns and no-ops if one already exists.
virtual void FixedUpdate (float fixedDeltaTime)
const List< GameObject * > & GetChildren () const
template<typename T>
T * GetComponent ()
 Finds the first attached component of type T, or nullptr.
GameObjectGetParent () const
const std::string & GetTag () const
bool HasChildren () const
template<typename T>
bool HasComponent ()
 True if a component of type T is attached.
bool HasParent () const
virtual void Initialize ()
 Initializes the object and its components; called once before the first Update.
bool IsActive () const
virtual bool IsLight () const
bool IsPendingDestroy () const
virtual void LateUpdate (float deltaTime)
void MarkForDestroy ()
 Flags the object for deferred destruction on the next scene pass.
void RemoveChild (GameObject *child)
template<typename T>
void RemoveComponent ()
 Destroys and detaches the first component of type T, if present.
void SetActive (bool active)
 Enables or disables the object, firing OnEnable/OnDisable on its components.
void SetParent (GameObject *parent)
 Reparents this object, updating both the old and new parent's child lists.
void SetTag (const std::string &tag)
virtual void Update (float deltaTime)
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)

Static Public Member Functions

static GameObjectCreateCapsule (Math::Vector3D position, int segments=16, int rings=8, float height=1, float radius=0.5)
static GameObjectCreateCube (Math::Vector3D position)
static GameObjectCreateCylinder (Math::Vector3D position, int segments=16, float height=1, float radius=0.5)
static GameObjectCreatePlane (Math::Vector3D position, int width=100, int height=100)
 Built-in primitive factories, mainly for prototyping and debug scenes.
static GameObjectCreateSphere (Math::Vector3D position, int stack=16, int slices=16)
static GameObjectCreateTorus (Math::Vector3D position, int segments=16, int rings=8, float innerRadius=8, float outerRadius=9)

Protected Attributes

bool bIsInitialized

Detailed Description

Scene entity holding components and an optional parent/child transform hierarchy. Scene owns the instance; destroy via Scene::DestroyObject().

A GameObject on its own does nothing. Behavior comes from the components attached to it: a TransformComponent for placement, a MeshComponent and MaterialComponent to be drawn, a ColliderComponent and RigidbodyComponent to be simulated, and your own Component subclasses for gameplay.

AddComponent<T>() constructs the component in place, forwarding extra arguments to its constructor after the owner pointer. Only one component of a given type is allowed per object: a second AddComponent<T>() logs a warning and does nothing. GetComponent<T>() resolves through dynamic_cast, so it also matches subclasses.

The scene that received the object through SceneBase::AddObject() owns it. Never delete a GameObject. Use SceneBase::DestroyObject() for deferred destruction, which is what you want while iterating, or SceneBase::RemoveObject() for immediate destruction.

Camera, Light, and its subclasses all derive from GameObject, so they are added and found the same way as anything else.

Sleak::Math::Vector3D(0.0f, 1.0f, 0.0f));
crate->SetTag("Destructible");
crate->AddComponent<Sleak::MaterialComponent>(
crate->AddComponent<Sleak::RigidbodyComponent>(
if (auto* t = crate->GetComponent<Sleak::TransformComponent>()) {
t->SetScale(Sleak::Math::Vector3D(2.0f, 2.0f, 2.0f));
}
AddObject(crate); // the scene now owns it
static GameObject * CreateCube(Math::Vector3D position)
Represents the position, rotation, and scale of an entity in 3D space.
See also
Component, SceneBase, TransformComponent, MeshComponent, MaterialComponent, Camera

Definition at line 57 of file GameObject.hpp.

Constructor & Destructor Documentation

◆ GameObject()

Sleak::GameObject::GameObject ( const std::string & name = "GameObject")
inline

Definition at line 59 of file GameObject.hpp.

◆ ~GameObject()

Sleak::GameObject::~GameObject ( )
override

Definition at line 14 of file GameObject.cpp.

Member Function Documentation

◆ AddChild()

void Sleak::GameObject::AddChild ( GameObject * child)

Definition at line 115 of file GameObject.cpp.

◆ AddComponent()

template<typename T, typename... Args>
void Sleak::GameObject::AddComponent ( Args &&... args)
inline

Constructs and attaches a component of type T. Warns and no-ops if one already exists.

Definition at line 67 of file GameObject.hpp.

◆ CreateCapsule()

GameObject * Sleak::GameObject::CreateCapsule ( Math::Vector3D position,
int segments = 16,
int rings = 8,
float height = 1,
float radius = 0.5 )
static

Definition at line 186 of file GameObject.cpp.

◆ CreateCube()

GameObject * Sleak::GameObject::CreateCube ( Math::Vector3D position)
static

Definition at line 158 of file GameObject.cpp.

◆ CreateCylinder()

GameObject * Sleak::GameObject::CreateCylinder ( Math::Vector3D position,
int segments = 16,
float height = 1,
float radius = 0.5 )
static

Definition at line 200 of file GameObject.cpp.

◆ CreatePlane()

GameObject * Sleak::GameObject::CreatePlane ( Math::Vector3D position,
int width = 100,
int height = 100 )
static

Built-in primitive factories, mainly for prototyping and debug scenes.

Definition at line 144 of file GameObject.cpp.

◆ CreateSphere()

GameObject * Sleak::GameObject::CreateSphere ( Math::Vector3D position,
int stack = 16,
int slices = 16 )
static

Definition at line 172 of file GameObject.cpp.

◆ CreateTorus()

GameObject * Sleak::GameObject::CreateTorus ( Math::Vector3D position,
int segments = 16,
int rings = 8,
float innerRadius = 8,
float outerRadius = 9 )
static

◆ FixedUpdate()

void Sleak::GameObject::FixedUpdate ( float fixedDeltaTime)
virtual

Definition at line 52 of file GameObject.cpp.

◆ GetChildren()

const List< GameObject * > & Sleak::GameObject::GetChildren ( ) const
inline

Definition at line 136 of file GameObject.hpp.

◆ GetComponent()

template<typename T>
T * Sleak::GameObject::GetComponent ( )
inline

Finds the first attached component of type T, or nullptr.

Definition at line 97 of file GameObject.hpp.

◆ GetParent()

GameObject * Sleak::GameObject::GetParent ( ) const
inline

Definition at line 135 of file GameObject.hpp.

◆ GetTag()

const std::string & Sleak::GameObject::GetTag ( ) const
inline

Definition at line 131 of file GameObject.hpp.

◆ HasChildren()

bool Sleak::GameObject::HasChildren ( ) const
inline

Definition at line 140 of file GameObject.hpp.

◆ HasComponent()

template<typename T>
bool Sleak::GameObject::HasComponent ( )
inline

True if a component of type T is attached.

Definition at line 114 of file GameObject.hpp.

◆ HasParent()

bool Sleak::GameObject::HasParent ( ) const
inline

Definition at line 139 of file GameObject.hpp.

◆ Initialize()

void Sleak::GameObject::Initialize ( )
virtual

Initializes the object and its components; called once before the first Update.

Reimplemented in Sleak::Camera.

Definition at line 30 of file GameObject.cpp.

◆ IsActive()

bool Sleak::GameObject::IsActive ( ) const
inline

Definition at line 128 of file GameObject.hpp.

◆ IsLight()

virtual bool Sleak::GameObject::IsLight ( ) const
inlinevirtual

Reimplemented in Sleak::Light.

Definition at line 142 of file GameObject.hpp.

◆ IsPendingDestroy()

bool Sleak::GameObject::IsPendingDestroy ( ) const
inline

Definition at line 146 of file GameObject.hpp.

◆ LateUpdate()

void Sleak::GameObject::LateUpdate ( float deltaTime)
virtual

Definition at line 66 of file GameObject.cpp.

◆ MarkForDestroy()

void Sleak::GameObject::MarkForDestroy ( )
inline

Flags the object for deferred destruction on the next scene pass.

Definition at line 145 of file GameObject.hpp.

◆ RemoveChild()

void Sleak::GameObject::RemoveChild ( GameObject * child)

Definition at line 120 of file GameObject.cpp.

◆ RemoveComponent()

template<typename T>
void Sleak::GameObject::RemoveComponent ( )
inline

Destroys and detaches the first component of type T, if present.

Definition at line 83 of file GameObject.hpp.

◆ SetActive()

void Sleak::GameObject::SetActive ( bool active)

Enables or disables the object, firing OnEnable/OnDisable on its components.

Definition at line 80 of file GameObject.cpp.

◆ SetParent()

void Sleak::GameObject::SetParent ( GameObject * parent)

Reparents this object, updating both the old and new parent's child lists.

Definition at line 99 of file GameObject.cpp.

◆ SetTag()

void Sleak::GameObject::SetTag ( const std::string & tag)
inline

Definition at line 130 of file GameObject.hpp.

◆ Update()

void Sleak::GameObject::Update ( float deltaTime)
virtual

Reimplemented in Sleak::Camera.

Definition at line 38 of file GameObject.cpp.

Member Data Documentation

◆ bIsInitialized

bool Sleak::GameObject::bIsInitialized
protected

Definition at line 157 of file GameObject.hpp.