Tutorial 6 – Components

We already discussed that GNodes are the building blocks of Genome2D’s render graph, they are just abstract entities not having any actual functionality. Thats where components come into play, they can’t be added to the render graph themselves but onto a specific node and will add their functionality to it. A single node can have any number of components at any time, with the exception of IRenderables that will be discussed later.

To add or manage components on node here are the calls.

// Add a component of specified class to a node instance, optionally you can specify lookup class that will be used for a component lookup instead of the original class
nodeInstance.addComponent(ComponentClass, ComponentLookupClass);

// Check if a node has a specified component
nodeInstance.hasComponent(ComponentLookupClass);

// Remove specified component from a node
nodeInstance.removeComponent(ComponentLookupClass);

// Get component instance on a node if exists
nodeInstance.getComponent(ComponentLookupClass);

All components need to extend GComponent class but after that its pretty much up to them when it comes to functionality. Each time you add a component to a node using the addComponent method you specify a component Class that will be instantiated as a component on that node, once you remove it using removeComponent the component will be disposed and the instance will no longer exist.

To create your own component its simple just create a class that extends GComponent

public class CustomComponent extends GComponent
{
   // This method is called once right after component is created and added to a node
   override public function init():void {
   }

   // This method is called when component is disposed (node.removeComponent or node.dispose)
   override public function dispose():void {
   }
}