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

Class responsible for registering and then creating GameObjects eg. Player, CameraObject. It is needed to load GameObjects from scene files but not all GameObjects can be registered. It requires your GameObject to not change constructor parameters. More...

#include <GameObjectFactory.hpp>

Public Types

using ObjectConstructor = GameObject* (*)(Engine&, const std::string&)
using Creator = std::function<GameObject*(Engine&, const std::string&)>

Public Member Functions

template<typename T>
void registerClass (const std::string &name, bool isDynamic=false)
 Register a GameObject class with the factory.
GameObjectcreate (const std::string &type, Engine &engine, const std::string &name)
 Create a GameObject instance of the specified type string.
std::vector< std::string > GetAllRegisteredObjectTypes ()
 Get all registered object types.
std::vector< std::string > GetNonDynamicRegisteredObjectTypes ()
 Get all non-dynamic registered object types.
std::vector< std::string > GetDynamicRegisteredObjectTypes ()
 Get all dynamic registered object types.
void clearDynamicGameObjects ()
 Clears all GameObjects registered as dynamic.
void UnregisterGameObjects ()
 Unregisters all GameObjects and clears internal lists.

Static Public Member Functions

static GameObjectFactory & getInstance ()
 Get the singleton instance of the GameObjectFactory.
template<typename T>
static GameObjectconstructObject (Engine &engine, const std::string &name)
 constructs registered objects, used internally in scene loading.

Private Member Functions

void registerCreator (const std::string &name, ObjectConstructor ctor, bool isDynamic)
 Registers a creator function for a specific GameObject type string.

Private Attributes

std::unordered_map< std::string, Creator > creators
std::vector< std::string > dynamicTypes
std::vector< std::string > nonDynamicTypes
std::vector< std::string > allTypes

Detailed Description

Class responsible for registering and then creating GameObjects eg. Player, CameraObject. It is needed to load GameObjects from scene files but not all GameObjects can be registered. It requires your GameObject to not change constructor parameters.

Definition at line 23 of file GameObjectFactory.hpp.

Member Typedef Documentation

◆ Creator

using vex::GameObjectFactory::Creator = std::function<GameObject*(Engine&, const std::string&)>

Definition at line 27 of file GameObjectFactory.hpp.

◆ ObjectConstructor

using vex::GameObjectFactory::ObjectConstructor = GameObject* (*)(Engine&, const std::string&)

Definition at line 25 of file GameObjectFactory.hpp.

Member Function Documentation

◆ clearDynamicGameObjects()

void vex::GameObjectFactory::clearDynamicGameObjects ( )

Clears all GameObjects registered as dynamic.

Used during hot-reload. Iterates through dynamicTypes, removes them from the creators map, and resets the allTypes list.

Definition at line 48 of file GameObjectFactory.cpp.

◆ constructObject()

template<typename T>
GameObject * vex::GameObjectFactory::constructObject ( Engine & engine,
const std::string & name )
inlinestatic

constructs registered objects, used internally in scene loading.

Definition at line 34 of file GameObjectFactory.hpp.

◆ create()

GameObject * vex::GameObjectFactory::create ( const std::string & type,
Engine & engine,
const std::string & name )

Create a GameObject instance of the specified type string.

Looks up the creator in the creators map. If found, invokes it. If not found, logs an error and returns nullptr.

Parameters
conststd::string& type - The registered type string.
Engine&engine - The engine instance to pass to the constructor.
conststd::string& name - The name for the new GameObject instance.
Returns
GameObject* - Pointer to the created object or nullptr on failure.

Definition at line 27 of file GameObjectFactory.cpp.

◆ GetAllRegisteredObjectTypes()

std::vector< std::string > vex::GameObjectFactory::GetAllRegisteredObjectTypes ( )

Get all registered object types.

Returns
std::vector<std::string> The registered object types.

Definition at line 36 of file GameObjectFactory.cpp.

◆ GetDynamicRegisteredObjectTypes()

std::vector< std::string > vex::GameObjectFactory::GetDynamicRegisteredObjectTypes ( )

Get all dynamic registered object types.

Returns
std::vector<std::string> The dynamic registered object types.

Definition at line 44 of file GameObjectFactory.cpp.

◆ getInstance()

GameObjectFactory & vex::GameObjectFactory::getInstance ( )
static

Get the singleton instance of the GameObjectFactory.

Definition at line 6 of file GameObjectFactory.cpp.

◆ GetNonDynamicRegisteredObjectTypes()

std::vector< std::string > vex::GameObjectFactory::GetNonDynamicRegisteredObjectTypes ( )

Get all non-dynamic registered object types.

Returns
std::vector<std::string> The non-dynamic registered object types.

Definition at line 40 of file GameObjectFactory.cpp.

◆ registerClass()

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

Register a GameObject class with the factory.

Parameters
std::string&name The name of the GameObject class.
boolisDynamic Whether the GameObject class is dynamic.

Definition at line 42 of file GameObjectFactory.hpp.

◆ registerCreator()

void vex::GameObjectFactory::registerCreator ( const std::string & name,
ObjectConstructor ctor,
bool isDynamic )
private

Registers a creator function for a specific GameObject type string.

Stores a lambda in the creators map that constructs the object and sets its objectType. Appends the name to allTypes and either dynamicTypes or nonDynamicTypes based on the flag.

Parameters
conststd::string& name - The string identifier for the class.
ObjectConstructorctor - Function pointer to the constructor.
boolisDynamic - True if this class is part of a hot-reloadable module.

Definition at line 11 of file GameObjectFactory.cpp.

◆ UnregisterGameObjects()

void vex::GameObjectFactory::UnregisterGameObjects ( )

Unregisters all GameObjects and clears internal lists.

Clears creators, dynamicTypes, nonDynamicTypes, and allTypes.

Definition at line 59 of file GameObjectFactory.cpp.

Member Data Documentation

◆ allTypes

std::vector<std::string> vex::GameObjectFactory::allTypes
private

Definition at line 90 of file GameObjectFactory.hpp.

◆ creators

std::unordered_map<std::string, Creator> vex::GameObjectFactory::creators
private

Definition at line 87 of file GameObjectFactory.hpp.

◆ dynamicTypes

std::vector<std::string> vex::GameObjectFactory::dynamicTypes
private

Definition at line 88 of file GameObjectFactory.hpp.

◆ nonDynamicTypes

std::vector<std::string> vex::GameObjectFactory::nonDynamicTypes
private

Definition at line 89 of file GameObjectFactory.hpp.


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