VR-Engage  2.3
Loading...
Searching...
No Matches
Entity Health Bars Example

Overview

Purpose: This example demonstrates how to extend DtEntityOverlayComponent to enrich entity overlays with per-entity health data. The custom component tracks entities in the player-station overlay pipeline and publishes a health value that downstream QML or overlay logic can render as a health bar or status label.

Observable Behavior: When the example is loaded in VR-Engage with the example simulation model set, entity overlays include additional health information for tracked entities. Human entities use the stateProperties.OverallHealth attribute, while other entities fall back to their damageState value and convert it to a percentage-style health estimate.

Prerequisites: Familiarity with the VR-Engage Player Station framework, entity overlay components, and the role/component registration pattern for frontend plugins. A working understanding of DtEntityOverlayComponent is useful when adapting the example to a custom UI. Use the example simulation model set and role files included with this example for the intended runtime setup.

Related Examples: See QML HUD for an example of QML-driven overlay data binding and Entity Detection for another frontend-plus-backend overlay pattern.


Key Concepts Demonstrated

This example demonstrates the minimal pattern for custom entity overlays:

  1. Component Registration - The plugin registers a custom component type with the player station component factory.
  2. Overlay Extension - The component subclasses DtEntityOverlayComponent and overrides the data population hook.
  3. Attribute Tracking - It requests the attributes needed to evaluate entity health from the entity attribute tree and keeps those values synchronized.
  4. Health Derivation - It supports both humanoid entities that expose stateProperties.OverallHealth and non-humanoid entities that represent health as damageState.

The implementation is intentionally small. It does not introduce new network messages or backend simulation logic; it adds data to the existing overlay pipeline so that the UI layer can decide how to render it.


Code Walkthrough

Component Registration

The plugin entry point registers the custom overlay component with VR-Engage's component factory:

// File: examples/entityHealthBars/plugin.cxx
extern "C"
{
{
return true;
}
}
Example overlay component that augments entity overlays with health values.
Definition entityHealthBarOverlay.h:10
void addCreator(std::string name="")
Registers a creator for a specific type.
Definition factory.h:129
Top-level class representing the VR-Engage application.
Definition playerStationApp.h:155
virtual DtComponentFactory & componentFactory()
Gets the component factory.
constexpr const char * DtEntityHealthBarOverlayType
Factory type name used to register the health-bar overlay component.
Definition entityHealthBarOverlay.h:6
VRECOMMONCOMPONENTS_DLL bool initPlayerStationModule(makVre::DtPlayerStationApp *app)
Module initialization function declarations using C linkage.

The component type string DtEntityHealthBarOverlayType is defined in the header and must match the type name used when the component is instantiated from role configuration.

Custom Overlay Type

The example defines a small component subclass that adds only the health-data behavior required for overlays:

// File: examples/entityHealthBars/entityHealthBarOverlay.h
constexpr const char* DtEntityHealthBarOverlayType = "DtEntityHealthBarOverlay";
{
public:
protected:
makVre::DtAttributeHandle& entityRootAttr) override;
};
virtual void populateEntityData(makVre::EntityOverlayInfo &info, makVre::DtAttributeHandle &entityRootAttr) override
Populates per-entity overlay data with health information.
virtual bool initialize(makVre::DtPlayerStation *player, makVre::DtInitTable &config) override
Initializes the overlay and registers required entity attributes.
Handle class for safe access to attributes.
Definition attributeHandle.h:30
QML overlay component that displays items above tracked entities.
Definition entityOverlayComponent.h:100
Table-based access to Lua state for configuration data.
Definition initializer.h:38
virtual DtPlayerStation & player()
Gets the player station.
Class for managing the user interface for engaged roles.
Definition playerStation.h:51
Simple structure to track individual entity overlay information.
Definition entityOverlayComponent.h:47

This keeps the example focused on the overlay contract rather than adding unrelated player-station logic.

Attribute Initialization

The component adds the attributes it needs to the overlay pipeline during initialization:

// File: examples/entityHealthBars/entityHealthBarOverlay.cpp
{
if (!DtEntityOverlayComponent::initialize(player, config))
{
return false;
}
myExternalEntityAttributes.emplace("stateProperties.OverallHealth");
myExternalEntityAttributes.emplace("damageState");
return true;
}
std::unordered_set< std::string > myExternalEntityAttributes
Attribute paths to keep synchronized for tracked external entities.
Definition entityOverlayComponent.h:193

This ensures the component keeps the required values synchronized for each tracked entity. The overlay base class handles the broader entity tracking and projection steps; this example only requests the specific attributes it needs.

Populating Overlay Data

The override method adds a health entry to the entity overlay payload:

// File: examples/entityHealthBars/entityHealthBarOverlay.cpp
makVre::DtAttributeHandle& entityRootAttr)
{
DtEntityOverlayComponent::populateEntityData(info, entityRootAttr);
makVre::DtAttributeHandle humanHealthAttr = entityRootAttr["stateProperties.OverallHealth"];
if (humanHealthAttr->isSet())
{
info.extraData["health"] = humanHealthAttr->get<int>();
}
else
{
switch (entityRootAttr["damageState"]->get<DtDamageState>())
{
case DtDamageNone:
info.extraData["health"] = 100.0;
break;
case DtDamageSlight:
info.extraData["health"] = 75.0;
break;
case DtDamageModerate:
info.extraData["health"] = 50.0;
break;
case DtDamageDestroyed:
info.extraData["health"] = 0.0;
break;
}
}
}
bool isSet() const
Checks if the attribute value is undefined.
const T get() const
Gets the value of the attribute.
QVariantMap extraData
Additional per-entity fields consumed by specialized overlays.
Definition entityOverlayComponent.h:54
double additionalHeightAboveEntity
Vertical screen offset (pixels) applied to the overlay position.
Definition entityOverlayComponent.h:52

This is the core behavior: the base class computes the entity's screen-space position, and this override adds a numeric health value into EntityOverlayInfo::extraData. The actual rendering of the bar or label is up to the QML or overlay consumer that reads the data model.


Architecture

This example is a frontend plugin only. It does not add a simulation-side actuator or custom message type. Instead, it depends on the existing overlay pipeline defined by DtEntityOverlayComponent in include/commonRoleLibraries/vreCommonComponents/entityOverlayComponent.h:

  • DtEntityOverlayComponent tracks entities and projects their positions to screen space.
  • EntityOverlayInfo carries the overlay state and per-entity metadata.
  • extraData exposes custom properties such as health for QML or other renderers.

The plugin therefore behaves as a data-provider for the broader overlay framework, rather than as a standalone renderer.


Deployment and Testing

Installation

Build the example target from the examples solution:

cmake --build . --config RelWithDebInfo --target entityHealthBars

Install the plugin for the current VR-Engage build:

cmake --install . --config RelWithDebInfo

The CMake target installs the plugin to the VR-Engage plugin directory:

  • <VR-Engage-Install-Dir>\plugins64\vrEngage\release\exampleEntityHealthBars.dll

Running the Example

Please run this example with the provided SMS which is entityHealthBar.sms. The example role file is located at:

  • data/data/simulationModelSets/examples/entityHealthBar/roles/militaryHumanWithOverlays.lua

The asset also references the example QML overlay file at data/UI/examples/exampleEntityOverlay.qml.

Use the example model set when launching VR-Engage:

vrEngage.exe -c -n 3 --setting "playerStationApp.defaultSimulationModelSets = {'<VR-Engage-Install-Dir>/data/simulationModelSets/examples/entityHealthBar.sms'};"

The exact SMS name present in the example data directory is the example model set for this scenario; it contains a human role that uses the overlay component. This is the intended setup for verifying the example in the same way the example role is authored.

Example Role Configuration

The example role adds the new component in the role definition by setting the component type to DtEntityHealthBarOverlay and pointing to the QML file used by the overlay:

-- File: data/data/simulationModelSets/examples/entityHealthBar/roles/militaryHumanWithOverlays.lua
inherits = "@(vre-roles-dir)/human.lua";
components = {
["entityOverlay"] = {
componentType = "DtEntityHealthBarOverlay";
qmlFilename = "examples/exampleEntityOverlay.qml";
qmlDataItemObjectName = "stateItem";
bindQmlPropertyToAttribute = {};
};
};

This entry is what makes the player station instantiate the DtEntityHealthBarOverlay component for the role. The qmlFilename property tells the overlay framework which QML item to populate, while qmlDataItemObjectName identifies the target object in that file. The example therefore combines the C++ overlay component with a role configuration that attaches it to a human entity.

Testing Procedure

  1. Build and install the entityHealthBars plugin.
  2. Launch VR-Engage using the example simulation model set for entityHealthBar.
  3. Load a scenario or startup configuration that uses the militaryHumanWithOverlays human role.
  4. Verify the overlay data is present for each tracked entity through the player-station overlay pipeline.

Expected Behavior:

  • Human entities populate health from stateProperties.OverallHealth.
  • Non-human entities derive health from damageState values.
  • additionalHeightAboveEntity is set for humanoid entities so the overlay is offset above the entity.

Troubleshooting

No health values appear:

  • Confirm the plugin loaded successfully from the VR-Engage plugin directory.
  • Verify the tracked entity exposes either stateProperties.OverallHealth or a valid damageState attribute.
  • Check the VR-Engage log for plugin registration or attribute lookup issues.

Overlay not displayed:

  • Confirm the role configuration includes the DtEntityHealthBarOverlay component.
  • Verify the overlay consumer in the active UI reads the health field from EntityOverlayInfo::extraData.