Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
Component Class Referenceabstract

Base class for all components that can be attached to GameObjects. More...

#include <component.h>

Inheritance diagram for Component:
[legend]

Public Member Functions

 Component ()
 
virtual ~Component ()=default
 
bool active () const noexcept
 Checks if the component is active.
 
Componentactive (bool value) noexcept
 
bool marked_for_deletion () const noexcept
 Activates the component.
 
Componentmark_for_deletion () noexcept
 
virtual void update (float dt)=0
 
virtual void on_attach ()
 Called when the component is attached to a GameObject.
 
virtual void on_detach ()
 Called when the component is detached from a GameObject.
 
virtual void on_serialize (std::vector< uint8_t > &) const
 Serializes the component's state into a byte array.
 
virtual void on_deserialize (const std::vector< uint8_t > &, size_t &)
 Deserializes the component's state from a byte array.
 
virtual std::string type_name () const =0
 Provides a consistent type name for the component.
 
const std::optional< std::reference_wrapper< GameObject > > & parent () const noexcept
 Gets the parent GameObject of this component.
 
std::optional< std::reference_wrapper< GameObject > > & parent () noexcept
 Gets the parent GameObject of this component.
 
Componentparent (GameObject &parent)
 
Componentparent (std::nullopt_t nullopt)
 
size_t add_on_attach (const std::function< void(Component &)> &action)
 Retrieves the type information of the component.
 
void remove_on_attach (size_t index)
 
size_t add_on_detach (const std::function< void(Component &)> &action)
 Adds an action to be performed when the component is attached.
 
void remove_on_detach (size_t index)
 

Detailed Description

Base class for all components that can be attached to GameObjects.

Warning
All derived components must implement the type_name() method to provide a consistent and portable type identifier for serialization purposes.

This is crucial for ensuring that component types can be accurately identified across different compilers and platforms, as relying on typeid(T).name() can lead to inconsistencies.

Note
this class should not be used directly. Instead, create derived component classes that inherit from Component.

Constructor & Destructor Documentation

◆ Component()

Component::Component ( )
explicit

◆ ~Component()

virtual Component::~Component ( )
virtualdefault

Member Function Documentation

◆ active() [1/2]

bool Component::active ( ) const
noexcept

Checks if the component is active.

Returns
whether the component is active.

◆ active() [2/2]

Component & Component::active ( bool  value)
noexcept

◆ add_on_attach()

size_t Component::add_on_attach ( const std::function< void(Component &)> &  action)

Retrieves the type information of the component.

Parameters
actionThe action to be performed on attach.
Returns
A reference to the std::type_info object representing the component's type.

◆ add_on_detach()

size_t Component::add_on_detach ( const std::function< void(Component &)> &  action)

Adds an action to be performed when the component is attached.

Parameters
actionThe action to be performed on attach.
Returns
the index of the added action, which can be used to remove it later.

◆ mark_for_deletion()

Component & Component::mark_for_deletion ( )
noexcept

◆ marked_for_deletion()

bool Component::marked_for_deletion ( ) const
noexcept

Activates the component.

Returns
A reference to this Component for chaining.

◆ on_attach()

virtual void Component::on_attach ( )
virtual

Called when the component is attached to a GameObject.

Reimplemented in AudioSource, and BehaviorScript.

◆ on_deserialize()

virtual void Component::on_deserialize ( const std::vector< uint8_t > &  ,
size_t &   
)
virtual

Deserializes the component's state from a byte array.

Parameters
dataThe byte array containing the serialized state.
offsetThe current offset in the byte array from which to start reading. This offset will be updated as bytes are read.
Returns
void

Reimplemented in NetworkIdentity, BehaviorScript, and Rigidbody2D.

◆ on_detach()

virtual void Component::on_detach ( )
virtual

Called when the component is detached from a GameObject.

Reimplemented in AudioSource, and BehaviorScript.

◆ on_serialize()

virtual void Component::on_serialize ( std::vector< uint8_t > &  ) const
virtual

Serializes the component's state into a byte array.

Parameters
outA vector to which the serialized bytes will be appended.
Returns
A vector of bytes representing the serialized state of the component.

Reimplemented in NetworkIdentity, BehaviorScript, and Rigidbody2D.

◆ parent() [1/4]

const std::optional< std::reference_wrapper< GameObject > > & Component::parent ( ) const
noexcept

Gets the parent GameObject of this component.

Returns
An optional reference to the parent GameObject, if it exists.

◆ parent() [2/4]

std::optional< std::reference_wrapper< GameObject > > & Component::parent ( )
noexcept

Gets the parent GameObject of this component.

Parameters
parentThe GameObject to set as the parent.
Returns
An optional reference to the parent GameObject, if it exists.

◆ parent() [3/4]

Component & Component::parent ( GameObject parent)

◆ parent() [4/4]

Component & Component::parent ( std::nullopt_t  nullopt)

◆ remove_on_attach()

void Component::remove_on_attach ( size_t  index)

◆ remove_on_detach()

void Component::remove_on_detach ( size_t  index)

◆ type_name()

virtual std::string Component::type_name ( ) const
pure virtual

Provides a consistent type name for the component.

Returns
A string representing the type name of the component. @CAUTION: All derived components must implement this method to ensure consistent serialization across different compilers and platforms.

Implemented in Renderable, AIController, NavigationGraph, NavigationNode, Pathfinding, Animator, AudioSource, BehaviorScript, BoxCollider2D, CircleCollider2D, Collider2D, NetworkIdentity, Rigidbody2D, Sprite, Image, and Text.

◆ update()


The documentation for this class was generated from the following file: