Table of Contents

So far we have talked about using built-in components like Camera and Renderable, but another major way you'll be using components is to create your own. Components serve as the main place to put your gameplay logic in, and this is where you'll be adding a majority of your custom code when creating a game.

Creation

To create your own component simply implement the Component interface by deriving from it. Component's constructor must always accept a handle to a SceneObject, which represents the scene object the component is part of.

Logic

Each component implementation can override any of the three primary methods for introducing gameplay logic:

Component::onInitialized - Called once when the component is first instantiated. You should use this instead of the constructor for initialization.

Component::update - Called every frame while the game is running and the component is enabled.

Component::fixedUpdate - Similar to update() except it gets called at a fixed time interval (e.g. 60 times per second, instead of every frame). Normally you want to use this method for physics-related functionality, which requires fixed time increments in order to ensure stability of calculations.

Component::onDestroyed - Called just before the component is destroyed. Use this instead of the destructor for cleanup.

Here is a simple implementation of a component using a few of these methods to implement basic camera movement.

// A simple component that moves a Camera component attached to the same SceneObject

HCamera mCamera; // Camera component that is influenced by this component.

// Virtual keys we will use for movement

VirtualButton mMoveForward;

VirtualButton mMoveBack;

VirtualButton mMoveLeft;

VirtualButton mMoveRight;

};

Use Component::SO() to access the scene object the component is attached to.

gTime() method provides access to a variety of timing related functionality, and is explained later in the timing manual.

Component handle

You will also likely want to declare a handle you can use to easily access the component, same as HCamera or HRenderable. This is done by simply creating a typedef on the GameObjectHandle<T> object.

typedef GameObjectHandle<CCameraFlyer> HCameraFlyer;

Using the component

We now have everything ready to use the component. You can create the component as any other by adding it to the scene object.

// Create a scene object to add our component to

HSceneObject cameraSO = SceneObject::create("Camera");

// We create a Camera component since our component relies on it

HCamera camera = cameraSO->addComponent<CCamera>(primaryWindow);

// And finally we add our component

HCameraFlyer = cameraSO->addComponent<CCameraFlyer>();

Data

Often a component will contain some data which you want to persist after the scene is saved, which requires you to implement a special interface on your custom component class. We'll talk more about this in the next chapter.

Activating/deactivating a scene object

Any scene object can be temporarily de-activated and reactivated by calling SceneObject::setActive(). When a scene object is deactivated its components will not have Component::update() called.

Your component can also be notified at the exact moment when activation/deactivation happens. This way you can perform additional functionality if needed. Override Component::onEnabled and Component::onDisabled to get notified every time a component is activated and deactivated, respectively.

TransformChangedFlags parameter will notify you whether the scene object moved, has changed parents, or both.

Note that Component::onTransformChanged will never trigger by default. You must first enable it by calling Component::setNotifyFlags(). It accepts the same TransformChangedFlags parameter which tells the system in which cases should it trigger Component::onTransformChanged.