Latest version:
Tramway SDK 0.0.9
Github
Quick links
Home
Get Started

EntityComponent


EntityComponents are re-usable parts for assembling Entities. Need to display a 3D model for an entity? Use a RenderComponent. Need to add entity to the physics simulation? Use a PhysicsComponent. Have an entity that simulates a frog and want to add frog behavior to other entities? Refactor the frog code into a FrogComponent.

Most components are created in a way that you can use them without attaching them to an entity, so you can use a RenderComponent to render some UI elements, or a skybox or something like that, but in general they are used for constructing entities.

Components can have dependencies on Resources. This can be automatically tracked.

Unlike entities, which can be repeatedly loaded and unloaded, once a component is initialized, it can only be deleted. After that it will have to be reconstructed and reinitialized.

Programming in C++


#include <framework/entitycomponent.h>
API documentation page.

Components work something like this:

  1. The component class object is constructed.
  2. The component is fed parameters by calling methods on it.
    • If the parameter is a Resource and it is unloaded, then it gets added to the Async loader queue.
    • In such a case, the component will remember that it is waiting for a resource.
  3. The component is initialized by calling the Init() method.
  4. If the component is waiting on any resources, then it will just do nothing and will wait for them to be ready.
  5. The component is started by either calling its own Start() method, if it isn't waiting on any resources, or by the Async system, once the resources on which the component is waiting have been loaded.
  6. The component does its component stuff. Some parameters may be changed by getting their methods called.
  7. The component class gets destructed.

None of the constructors have any parameters – this is mostly due to the fact that I feel like the initialization code for components with a lot of parameters, and therefore very long constructors looks incredibly awful.

If you want to create your own component, you can copy the header and implementation files of the TemplateComponent.

Component<TemplateComponent> template;

// constructs a new TemplateComponent
template.make();

// initializes it
template->Init();

// destructs it
template.clear();

The Component smart pointer class ensures that it will be constructed into the correct Pool. This is required for some components to work properly, like the AnimationComponent, which will not get updated if it isn't constructed in the correct pool.