VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
vex::ComponentRegistry Class Reference

Class registering GameComponents mainly used to load them in SceneManager. More...

#include <ComponentFactory.hpp>

Public Types

using ComponentLoader = std::function<void(GameObject&, const nlohmann::json&)>
using ComponentSaver = std::function<nlohmann::json(GameObject&)>
using ComponentInspector = std::function<void(GameObject&)>
using ComponentChecker = std::function<bool(const GameObject&)>
using ComponentCreator = std::function<void(GameObject&)>

Public Member Functions

template<typename T>
void registerComponent (const std::string &name, bool isDynamic=false)
 Template function to register a component type with the system.
void unregisterComponent (const std::string &name)
 Unregisters a component type and removes all associated callbacks.
void clearDynamicComponents ()
 Clears all components registered as dynamic.
bool loadComponent (GameObject &obj, const std::string &type, const nlohmann::json &json)
 Loads data into a component on a GameObject from JSON.
nlohmann::json saveComponent (GameObject &obj, const std::string &type)
 Serializes a component on a GameObject to JSON.
void drawInspectorForObject (GameObject &obj)
 Draws the ImGui inspector for all components on the object.
void createComponent (GameObject &obj, const std::string &name)
 Creates and attaches a specific component to a game object.
std::vector< std::string > getMissingComponents (const GameObject &obj)
 Get missing components for a game object.
const std::unordered_map< std::string, ComponentInspector > & getInspectors () const
 Get component inspectors.
const std::vector< std::string > & getRegisteredNames () const
 Get registered component names.

Static Public Member Functions

static ComponentRegistry & getInstance ()

Private Attributes

std::vector< std::string > registeredNames
std::vector< std::string > dynamicComponents
std::unordered_map< std::string, ComponentLoader > loaders
std::unordered_map< std::string, ComponentSaver > savers
std::unordered_map< std::string, ComponentInspector > inspectors
std::unordered_map< std::string, ComponentChecker > checkers
std::unordered_map< std::string, ComponentCreator > creators

Detailed Description

Class registering GameComponents mainly used to load them in SceneManager.

Definition at line 298 of file ComponentFactory.hpp.

Member Typedef Documentation

◆ ComponentChecker

using vex::ComponentRegistry::ComponentChecker = std::function<bool(const GameObject&)>

Definition at line 303 of file ComponentFactory.hpp.

◆ ComponentCreator

using vex::ComponentRegistry::ComponentCreator = std::function<void(GameObject&)>

Definition at line 304 of file ComponentFactory.hpp.

◆ ComponentInspector

using vex::ComponentRegistry::ComponentInspector = std::function<void(GameObject&)>

Definition at line 302 of file ComponentFactory.hpp.

◆ ComponentLoader

using vex::ComponentRegistry::ComponentLoader = std::function<void(GameObject&, const nlohmann::json&)>

Definition at line 300 of file ComponentFactory.hpp.

◆ ComponentSaver

using vex::ComponentRegistry::ComponentSaver = std::function<nlohmann::json(GameObject&)>

Definition at line 301 of file ComponentFactory.hpp.

Member Function Documentation

◆ clearDynamicComponents()

void vex::ComponentRegistry::clearDynamicComponents ( )

Clears all components registered as dynamic.

Used for hot-reloading. Iterates dynamicComponents and manually unregisters each one to clean up function pointers from unloaded DLLs.

Definition at line 43 of file ComponentFactory.cpp.

◆ createComponent()

void vex::ComponentRegistry::createComponent ( GameObject & obj,
const std::string & name )

Creates and attaches a specific component to a game object.

Uses the registered creator lambda to default-construct and add the component.

Parameters
GameObject&obj - The target object.
conststd::string& name - The component type name.

Definition at line 96 of file ComponentFactory.cpp.

◆ drawInspectorForObject()

void vex::ComponentRegistry::drawInspectorForObject ( GameObject & obj)

Draws the ImGui inspector for all components on the object.

Iterates through all registered inspectors and calls them on the object.

Parameters
GameObject&obj - The object to inspect.

Definition at line 80 of file ComponentFactory.cpp.

◆ getInspectors()

const std::unordered_map< std::string, ComponentRegistry::ComponentInspector > & vex::ComponentRegistry::getInspectors ( ) const

Get component inspectors.

Definition at line 104 of file ComponentFactory.cpp.

◆ getInstance()

ComponentRegistry & vex::ComponentRegistry::getInstance ( )
static

Definition at line 6 of file ComponentFactory.cpp.

◆ getMissingComponents()

std::vector< std::string > vex::ComponentRegistry::getMissingComponents ( const GameObject & obj)

Get missing components for a game object.

Definition at line 86 of file ComponentFactory.cpp.

◆ getRegisteredNames()

const std::vector< std::string > & vex::ComponentRegistry::getRegisteredNames ( ) const

Get registered component names.

Definition at line 108 of file ComponentFactory.cpp.

◆ loadComponent()

bool vex::ComponentRegistry::loadComponent ( GameObject & obj,
const std::string & type,
const nlohmann::json & json )

Loads data into a component on a GameObject from JSON.

Finds the registered loader for type and executes it. Logs an error if the type is not registered.

Parameters
GameObject&obj - The target object.
conststd::string& type - The component type name.
constnlohmann::json& json - The JSON data to load.
Returns
bool - True if successful, false if type not found.

Definition at line 65 of file ComponentFactory.cpp.

◆ registerComponent()

template<typename T>
void vex::ComponentRegistry::registerComponent ( const std::string & name,
bool isDynamic = false )
inline

Template function to register a component type with the system.

Generates and stores lambdas for:

  • Loading: Deserializes JSON into the component (creates it if missing).
  • Saving: Serializes component to JSON.
  • Inspection: Registers GenericComponentInspector<T> which uses ImGui and ImReflect to draw UI.
  • Checking: Checks if an object has this component.
  • Creation: Adds the default-constructed component to an object.
    Parameters
    conststd::string& name - The name of the component class.
    boolisDynamic - Whether this component is part of a hot-reloadable module.

Definition at line 318 of file ComponentFactory.hpp.

◆ saveComponent()

nlohmann::json vex::ComponentRegistry::saveComponent ( GameObject & obj,
const std::string & type )

Serializes a component on a GameObject to JSON.

Parameters
GameObject&obj - The source object.
conststd::string& type - The component type name.
Returns
nlohmann::json - The serialized data, or nullptr if not found/registered.

Definition at line 75 of file ComponentFactory.cpp.

◆ unregisterComponent()

void vex::ComponentRegistry::unregisterComponent ( const std::string & name)

Unregisters a component type and removes all associated callbacks.

Erases entries from loaders, savers, inspectors, checkers, and creators maps, and removes the name from registeredNames.

Parameters
conststd::string& name - The name of the component to unregister.

Definition at line 28 of file ComponentFactory.cpp.

Member Data Documentation

◆ checkers

std::unordered_map<std::string, ComponentChecker> vex::ComponentRegistry::checkers
private

Definition at line 416 of file ComponentFactory.hpp.

◆ creators

std::unordered_map<std::string, ComponentCreator> vex::ComponentRegistry::creators
private

Definition at line 417 of file ComponentFactory.hpp.

◆ dynamicComponents

std::vector<std::string> vex::ComponentRegistry::dynamicComponents
private

Definition at line 412 of file ComponentFactory.hpp.

◆ inspectors

std::unordered_map<std::string, ComponentInspector> vex::ComponentRegistry::inspectors
private

Definition at line 415 of file ComponentFactory.hpp.

◆ loaders

std::unordered_map<std::string, ComponentLoader> vex::ComponentRegistry::loaders
private

Definition at line 413 of file ComponentFactory.hpp.

◆ registeredNames

std::vector<std::string> vex::ComponentRegistry::registeredNames
private

Definition at line 411 of file ComponentFactory.hpp.

◆ savers

std::unordered_map<std::string, ComponentSaver> vex::ComponentRegistry::savers
private

Definition at line 414 of file ComponentFactory.hpp.


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