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
- You completed Create a C++ Game Plugin.
- The game plugin builds and loads in the editor.
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
- Add a new component header and source file to the plugin project.
- Derive the component from
plComponent. - Declare a component manager for the component.
- 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
- Build the plugin.
- Return to the editor.
- Reload the project or restart the editor if needed.
- Confirm that the component appears in the component menu.
Attach the Component
- Open the scene from Create Your First Scene.
- Select the test object.
- Add the new component.
- Set the editable property values.
- 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.