Skip to main content

Create Your First C++ Component

In this tutorial you add a simple reflected C++ component to the game plugin and attach it to an object in the scene. The component exposes editable properties so you can tune behavior from the editor.

Prerequisites

Goal

By the end, a scene object will have a custom C++ component that appears in the editor component menu and updates while the scene is running.

<IMG: Editor inspector showing a custom C++ component with editable properties>

Add a Component Class

  1. Add a new component header and source file to the plugin project.
  2. Derive the component from plComponent.
  3. Declare a component manager for the component.
  4. Add reflected properties for any values you want to edit in the editor.

For a first component, keep the behavior small. A good first test is moving the owner object up and down or changing a debug value over time.

Register Reflection

Add a reflection block in the component source file. Use a user-facing component category such as Gameplay so the component appears in a sensible place in the editor.

PL_BEGIN_COMPONENT_TYPE(FirstGameComponent, 1, plComponentMode::Dynamic)
{
PL_BEGIN_PROPERTIES
{
PL_MEMBER_PROPERTY("Amplitude", m_fAmplitude),
}
PL_END_PROPERTIES;

PL_BEGIN_ATTRIBUTES
{
new plCategoryAttribute("Gameplay"),
}
PL_END_ATTRIBUTES;
}
PL_END_COMPONENT_TYPE

Build and Reload

  1. Build the plugin.
  2. Return to the editor.
  3. Reload the project or restart the editor if needed.
  4. Confirm that the component appears in the component menu.

Attach the Component

  1. Open the scene from Create Your First Scene.
  2. Select the test object.
  3. Add the new component.
  4. Set the editable property values.
  5. Run the scene and verify that the component behavior is visible.

Troubleshooting

  • If the component does not appear, check that the plugin loaded and the component type is reflected.
  • If the object does not move or update, confirm the component manager update type and simulation state.
  • If exported scenes are planned, implement component serialization before relying on the component outside editor testing.

Next Step

Continue with Create Your First AngelScript Script to add script-driven gameplay logic.

See Also