libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Ship::TickableComponent Class Reference

Combines Tickable and Component, auto-registering with a Context. More...

#include <TickableComponent.h>

Inheritance diagram for Ship::TickableComponent:
[legend]
Collaboration diagram for Ship::TickableComponent:
[legend]

Public Member Functions

 TickableComponent (const std::string &name, std::shared_ptr< Context > context, const TickGroup tickGroup=TickGroup::TickGroupDefault, const TickPriority tickPriority=TickPriority::TickPriorityDefault, const std::vector< EventID > &eventIds={})
 Constructs a TickableComponent with EventID subscriptions.
 
 TickableComponent (const std::string &name, std::shared_ptr< Context > context, const TickGroup tickGroup, const TickPriority tickPriority, const std::vector< std::shared_ptr< Action > > &actions)
 Constructs a TickableComponent with an explicit list of Actions.
 
virtual ~TickableComponent ()
 
bool RegisterWithContext ()
 Registers this component with its Context.
 
void UnregisterFromContext ()
 Unregisters this component from its Context.
 
std::shared_ptr< ContextGetContext () const
 Returns the Context this component is registered with.
 
TickGroup GetTickGroup () const
 Returns the TickGroup this component belongs to.
 
TickPriority GetTickPriority () const
 Returns the tick priority within the TickGroup.
 
uint64_t GetOrder () const
 Returns a composite order value derived from TickGroup and TickPriority.
 
TickableComponentSetTickGroup (const TickGroup tickGroup)
 Sets the TickGroup for this component.
 
TickableComponentSetTickPriority (const TickPriority tickPriority)
 Sets the tick priority for this component.
 
TickableComponentSetContext (std::shared_ptr< Context > context)
 Changes the Context this component is associated with.
 
virtual bool ActionRan (EventID eventId)
 Virtual hook invoked when an EventAction runs on this component.
 
- Public Member Functions inherited from Ship::Tickable
 Tickable (const bool isTicking=true)
 Constructs a Tickable.
 
 Tickable (const bool isTicking, const std::vector< std::shared_ptr< Action > > &actions)
 Constructs a Tickable with an initial set of Actions.
 
virtual ~Tickable ()
 
bool IsTicking () const
 Returns true if this Tickable is currently ticking.
 
bool Start (const bool force=false)
 Starts ticking, enabling EventID-targeted Tick() calls on Actions.
 
bool Stop (const bool force=false)
 Stops ticking; subsequent Tick() calls become no-ops.
 
bool Tick (EventID eventId)
 Ticks only indexed EventActions whose EventID matches the given ID.
 
bool Tick (const std::vector< EventID > &eventIds)
 Ticks only indexed EventActions whose EventID matches one of the given IDs.
 
template<typename T >
bool Tick (const std::vector< EventID > &eventIds)
 Ticks Actions matching both type T and one of the given EventIDs.
 
template<typename T >
bool Tick (EventID eventId)
 Ticks Actions matching both type T and the given EventID.
 
EventActionListGetActionList ()
 Returns a mutable reference to the internal EventActionList.
 
const EventActionListGetActionList () const
 Returns a const reference to the internal EventActionList.
 
double GetTime (const ClockType clockType) const
 Returns the wall-clock time for a profiling checkpoint.
 
- Public Member Functions inherited from Ship::Component
 Component (const std::string &name, std::shared_ptr< Context > context=nullptr)
 Constructs a Component with the given name.
 
virtual ~Component ()
 
void Init (const nlohmann::json &initArgs=nlohmann::json::object())
 Performs one-time initialization of this component.
 
bool IsInitialized () const
 Returns true once Init() (or MarkInitialized()) has completed successfully.
 
const std::string & GetName () const
 Returns the name of this Component.
 
std::string ToString () const
 Returns a human-readable string representation (e.g. "Name (id)").
 
std::string ToTreeString (int depth=0) const
 Returns a human-readable tree representation of this component and its children.
 
 operator std::string () const
 Conversion operator to std::string; equivalent to ToString().
 
ParentComponentListGetParents ()
 Returns a mutable reference to the parent list.
 
const ParentComponentListGetParents () const
 Returns a const reference to the parent list.
 
ComponentListGetChildren ()
 Returns a mutable reference to the child list.
 
const ComponentListGetChildren () const
 Returns a const reference to the child list.
 
virtual std::shared_ptr< ComponentTryGetSharedComponent () noexcept
 Returns a shared_ptr to this Component when available, otherwise nullptr.
 
virtual std::shared_ptr< ComponentGetSharedComponent ()
 Returns a shared_ptr to this Component via the correct enable_shared_from_this base.
 
template<typename T >
bool HasInChildren () const
 Checks whether any descendant Component matches type T via BFS.
 
template<typename T >
std::shared_ptr< T > GetFirstInChildren () const
 Returns the first descendant that matches type T via BFS.
 
template<typename T >
std::shared_ptr< std::vector< std::shared_ptr< T > > > GetInChildren () const
 Returns all descendants that match type T via BFS.
 
template<typename T >
bool HasInParents () const
 Checks whether any ancestor Component matches type T via BFS.
 
template<typename T >
std::shared_ptr< T > GetFirstInParents () const
 Returns the first ancestor that matches type T via BFS.
 
template<typename T >
std::shared_ptr< std::vector< std::shared_ptr< T > > > GetInParents () const
 Returns all ancestors that match type T via BFS.
 
- Public Member Functions inherited from Ship::Part
 Part ()
 Constructs a Part and assigns it a unique ID.
 
 Part (std::shared_ptr< Context > context)
 Constructs a Part with an explicit Context reference and unique ID.
 
virtual ~Part ()=default
 
uint64_t GetId () const
 Returns the unique identifier for this Part.
 
bool operator== (const Part &other) const
 Compares two Parts for equality by their unique IDs.
 
std::shared_ptr< ContextGetContext () const
 Returns the Context this Part belongs to, or nullptr if unset.
 
void SetContext (std::shared_ptr< Context > ctx)
 Sets the Context this Part belongs to.
 

Additional Inherited Members

- Protected Member Functions inherited from Ship::Tickable
virtual bool CanStart ()
 Permission hook; override to prevent starting. Defaults to true.
 
virtual bool CanStop ()
 Permission hook; override to prevent stopping. Defaults to true.
 
virtual void Started (const bool forced)
 Notification hook called after the Tickable has been started.
 
virtual void Stopped (const bool forced)
 Notification hook called after the Tickable has been stopped.
 
std::mutex & GetMutex ()
 Returns a reference to the internal mutex for synchronization. Only available when COMPONENT_THREAD_SAFE is defined.
 
- Protected Member Functions inherited from Ship::Component
virtual void OnInit (const nlohmann::json &initArgs=nlohmann::json::object())
 Override this to implement component-specific initialization logic.
 
void MarkInitialized ()
 Marks this component as initialized without going through Init().
 
template<typename T >
std::shared_ptr< T > RequireDependency (const std::shared_ptr< T > &dependency, const std::string &dependencyName) const
 Returns a cached dependency after validating it exists and is initialized.
 
- Protected Member Functions inherited from Ship::Part
virtual void OnAdded (bool forced)
 Called after this Part has been added to a PartList.
 
virtual void OnRemoved (bool forced)
 Called after this Part has been removed from a PartList.
 

Detailed Description

Combines Tickable and Component, auto-registering with a Context.

TickableComponent is the primary building block for objects that need both a name/hierarchy (Component) and per-frame tick/draw execution (Tickable). After construction and shared_ptr ownership, call RegisterWithContext() to wire the component into the Context's tick loop.

Event names (e.g. "Tick", "Draw", "DrawDebugMenu") are registered dynamically with the Events component rather than using hardcoded enum values. When RegisterWithContext() is called, EventActions are created for each event name the component subscribes to.

Context TickableList semantics: A TickableComponent is present in the Context's TickableList (and therefore executed each frame) as long as it has at least one parent. It is added to the list when its first parent is assigned and removed when its last parent is removed. This is managed automatically by ComponentList when the COMPONENT_THREAD_SAFE or default parent/child relationship hooks fire.

Constructor & Destructor Documentation

◆ TickableComponent() [1/2]

Ship::TickableComponent::TickableComponent ( const std::string &  name,
std::shared_ptr< Context context,
const TickGroup  tickGroup = TickGroup::TickGroupDefault,
const TickPriority  tickPriority = TickPriority::TickPriorityDefault,
const std::vector< EventID > &  eventIds = {} 
)

Constructs a TickableComponent with EventID subscriptions.

Call RegisterWithContext() after the object is owned by a shared_ptr.

Parameters
nameHuman-readable name for the Component.
contextThe Context to register with.
tickGroupThe TickGroup this component belongs to.
tickPriorityExecution priority within the TickGroup.
eventIdsList of EventIDs to create EventActions for.

◆ TickableComponent() [2/2]

Ship::TickableComponent::TickableComponent ( const std::string &  name,
std::shared_ptr< Context context,
const TickGroup  tickGroup,
const TickPriority  tickPriority,
const std::vector< std::shared_ptr< Action > > &  actions 
)

Constructs a TickableComponent with an explicit list of Actions.

Parameters
nameHuman-readable name for the Component.
contextThe Context to register with.
tickGroupThe TickGroup this component belongs to.
tickPriorityExecution priority within the TickGroup.
actionsThe initial set of Actions to register.

◆ ~TickableComponent()

virtual Ship::TickableComponent::~TickableComponent ( )
virtual

Member Function Documentation

◆ ActionRan()

virtual bool Ship::TickableComponent::ActionRan ( EventID  eventId)
virtual

Virtual hook invoked when an EventAction runs on this component.

Subclasses override this to respond to specific EventIDs.

Parameters
eventIdThe EventID that executed.
Returns
True if the action executed successfully.

◆ GetContext()

std::shared_ptr< Context > Ship::TickableComponent::GetContext ( ) const

Returns the Context this component is registered with.

◆ GetOrder()

uint64_t Ship::TickableComponent::GetOrder ( ) const

Returns a composite order value derived from TickGroup and TickPriority.

◆ GetTickGroup()

TickGroup Ship::TickableComponent::GetTickGroup ( ) const

Returns the TickGroup this component belongs to.

◆ GetTickPriority()

TickPriority Ship::TickableComponent::GetTickPriority ( ) const

Returns the tick priority within the TickGroup.

◆ RegisterWithContext()

bool Ship::TickableComponent::RegisterWithContext ( )

Registers this component with its Context.

Must be called after the component has shared ownership and is reachable from the component hierarchy so self can be resolved internally. Creates EventActions for all pending event names.

Returns
True on successful registration, false when self cannot be resolved.

◆ SetContext()

TickableComponent & Ship::TickableComponent::SetContext ( std::shared_ptr< Context context)

Changes the Context this component is associated with.

Parameters
contextThe new Context.
Returns
A reference to this for chaining.

◆ SetTickGroup()

TickableComponent & Ship::TickableComponent::SetTickGroup ( const TickGroup  tickGroup)

Sets the TickGroup for this component.

Parameters
tickGroupThe new TickGroup.
Returns
A reference to this for chaining.

◆ SetTickPriority()

TickableComponent & Ship::TickableComponent::SetTickPriority ( const TickPriority  tickPriority)

Sets the tick priority for this component.

Parameters
tickPriorityThe new TickPriority.
Returns
A reference to this for chaining.

◆ UnregisterFromContext()

void Ship::TickableComponent::UnregisterFromContext ( )

Unregisters this component from its Context.


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