ComponentSystem Class
Header: | #include <UnigineComponentSystem.h> |
Inherits: | Unigine::WorldLogic |
This class implements functionality of the Custom Component System and is used to create, destroy, and manage components.
This class is a singleton.
See Also#
- Custom Component System usage example for more details on using the Custom Component System.
- C++ Sample: source/samples/Api/Logics/ComponentSystem
ComponentSystem Class
Members
ComponentSystem * get( )
Returns a pointer to the Component System. This pointer must be obtained to access functions of the Component System:ComponentSystem *cs = ComponentSystem::get();
// access functions of the Component System
...
void initialize( )
Performs initialization of the Component System, registration of all components and creation of all necessary property files.- This method should me called in the AppSystemLogic::init() method.
- If a property file for any component does not exist, it will be created automatically.
virtual int AppSystemLogic::init()
{
// initialize ComponentSystem and register all components
ComponentSystem::get()->initialize();
/*...*/
return 1;
}
int getNumComponents( )
Returns the total number of components registered in the Component System.This method is very slow and should not be used often.
Return value
Total number of registered components.int getNumNodesWithComponents( )
Returns the total number of nodes with components assigned.Return value
Total number of nodes with components assigned.template <C class>
void createPropertyFile( )
Creates a property file for a registered component associated with the property with a given name.Parameters of each component are stored in a separate *.prop file. If this property file does not exist, it will be created in the data/ComponentSystem folder with a name obtained by the component's getPropertyName() method.void createPropertyFile( const char * name )
Creates a property file for a registered component associated with the property with a given name. Parameters of each component are stored in a separate *.prop file. If this property file does not exist, it will be created in the data/ComponentSystem folder.Arguments
- const char * name - Name of the property associated with the component.
void createPropertyFiles( )
Creates property files for all registered components. Parameters of each component are stored in a separate *.prop file. If these property files do not exist, they will be created in the data/ComponentSystem folder.void refreshProperty( const char * name )
Rewrites the *.prop file for the specified property and reloads it in the Property Manager.Arguments
- const char * name - Property name.
template <C class>
void refreshProperty( )
Rewrites the property file for the specified component and reloads it in the Property Manager.template <C class>
void registerComponent( )
Registers a user component derived from the ComponentBase class.- Components must be registered in the AppSystemLogic::init() method.
- If a property file for the component does not exist, it will be created automatically.
virtual int AppSystemLogic::init()
{
// registering a new user component
ComponentSystem::get()->registerComponent<MyComponent>();
/*...*/
return 1;
}
template <C class>
C * addComponent( const NodePtr & node )
Adds the component to the specified node.When adding a component to a node do not forget to pass this node to the Editor to ensure its proper removal via destroyNode().
NodeDummyPtr node = NodeDummy::create();
node->setName("node_dummy");
// pass ownership to the Editor
node->release();
Editor::get()->addNode(node, 1);
ComponentSystem::get()->addComponent<MyComponent>(node->getNode());
// now the node named node_dummy appears in the Editor
Arguments
- const NodePtr & node - Node, to which the component is to be added.
Return value
Pointer to the new added component, if it was successfully added to the specified node; otherwise nullptr.template <C class>
C * getComponent( const NodePtr & node )
Returns the first component of the specified type associated with the given node.Arguments
- const NodePtr & node - Node, for which the component of the specified type is to be found.
Return value
Pointer to the component if any; otherwise, nullptr.template <C class>
void getComponents( const NodePtr & node, Vector<C *> & out_components, int clear_vector )
Searches for all components of the specified type down the hierarchy of the specified node and puts them to the given buffer vector.Arguments
- const NodePtr & node - Node, for which the components of the specified type are to be found.
- Vector<C *> & out_components - Buffer vector, to which all found components of the specified type will be added.
- int clear_vector - Flag indicating whether the buffer vector is to be cleared before adding the found components to it or not. Use 1 to clear the vector, 0 - to append new found components to the end of the vector. The default value is 1.
template <C class>
C * getComponentInChildren( const NodePtr & node )
Returns the first component of the specified type found among all the children of the specified node (including the node itself). This method searches for the component in the following order:- node itself
- node reference
- node's children
- children of node's children
Arguments
- const NodePtr & node - Node, whose hierarchy is to be checked for the specified type of component.
Return value
Pointer to the component if any; otherwise, nullptr.template <C class>
void getComponentsInChildren( const NodePtr & node, Vector<C *> & out_components, int clear_vector )
Searches for all components of the specified type down the hierarchy of the specified node and puts them to the given buffer vector.Arguments
- const NodePtr & node - Node, whose hierarchy is to be checked.
- Vector<C *> & out_components - Buffer vector, to which all found components of the specified type will be added.
- int clear_vector - Flag indicating whether the buffer vector is to be cleared before adding the found components to it or not. Use 1 to clear the vector, 0 - to append new found components to the end of the vector. The default value is 1.
template <C class>
C * getComponentInParent( const NodePtr & node )
Returns the first component of the specified type found among all predecessors and posessors of the specified node.Arguments
- const NodePtr & node - Node, whose hierarchy is to be checked.
Return value
Pointer to the component if any; otherwise, nullptr.template <C class>
void getComponentsInParent( const NodePtr & node, Vector<C *> & out_components, int clear_vector )
Searches for all components of the specified type up the hierarchy of the specified node and puts them to the given buffer vector.Arguments
- const NodePtr & node - Node, whose hierarchy is to be checked.
- Vector<C *> & out_components - Buffer vector, to which all found components of the specified type will be added.
- int clear_vector - Flag indicating whether the buffer vector is to be cleared before adding the found components to it or not. Use 1 to clear the vector, 0 - to append new found components to the end of the vector. The default value is 1.
template <C class>
int removeComponent( const NodePtr & node )
Removes the component from the specified node.// removes a user component MyComponent from the node
ComponentSystem::get()->removeComponent<MyComponent>(some_node);
Arguments
- const NodePtr & node - Node, from which the component is to be removed.
Return value
1 if the component was successfully removed from the specified node; otherwise 0.void destroyNode( const NodePtr & node )
Destroys the specified node. The node will be destroyed with all its properties and components.Make sure, that the node you're trying to delete is owned by the Editor or is an orphan (release() method was called). Anyway the node must not be owned by code, otherwise a crash will be imminent.
Arguments
- const NodePtr & node - Node to be destroyed.
void init( )
Engine calls this function on world initialization. Put you code for resources initialization during the world start here. This function is called by the world script's init() function.void update( )
Engine calls this function before updating each render frame. You can specify here all logic-related functions you want to be called every frame while your application executes. This function is called by the world script's update() function.void render( )
Engine calls this function before rendering each render frame. You can correct behavior after the state of the node has been updated. This function is called by the world script's render() function.void flush( )
Engine calls this function before updating each physics frame. This function is used to control physics. The engine calls flush() with the fixed rate (60 times per second by default) regardless of the fps number. This function is called by the world script's flush() function.void shutdown( )
Engine calls this function on world shutdown. Here you can delete resources that were created during world script execution to avoid memory leaks. This function is called by the world script's shutdown() function.void destroy( )
Engine calls this function when the video mode is changed or application is restarted (video_restart is called). It is used to destroy all created resources (e.g. dynamic textures, etc.) to avoid crashes due to invalid pointers, when the graphics context is re-initialized.int mkdir( const char * name )
Creates a new directory.Arguments
- const char * name - Path for a new directory to be created.
Return value
1 if the directory is created successfully; otherwise, 0.void setWarningLevel( int level )
Sets the warning level for the Component System. Warnings can be very useful when debugging your application, e.g. to investigate Null Reference Exception cases.Arguments
- int level - New warning level to be set. One of the following values:
- WARNING_LEVEL::NONE - warning messages are disabled.
- WARNING_LEVEL::LOW - warning messages are generated only for serious cases.
- WARNING_LEVEL::HIGH - warning messages are generated for all cases including potential ones. At this level, for example, all Node/Property/Material parameters, that are empty at startup, will be reported.
int getWarningLevel( )
Returns the current warning level for the Component System. Warnings can be very useful when debugging your application, e.g. to investigate Null Reference Exception cases.Return value
Current warning level. One of the following values:- WARNING_LEVEL::NONE - warning messages are disabled.
- WARNING_LEVEL::LOW - warning messages are generated only for serious cases.
- WARNING_LEVEL::HIGH - warning messages are generated for all cases including potential ones. At this level, for example, all Node/Property/Material parameters, that are empty at startup, will be reported.
Last update:
2018-12-27
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)