Put the public API at the top of the class
List public functions first, followed by protected extension points and private state.
#pragma once
#include "Components/ActorComponent.h"
#include "HealthComponent.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(
FHealthChangedSignature,
float, PreviousHealth,
float, CurrentHealth);
UCLASS(ClassGroup = (Combat), meta = (BlueprintSpawnableComponent))
class ACTIONGAME_API UHealthComponent final : public UActorComponent
{
GENERATED_BODY()
public:
UHealthComponent();
UFUNCTION(BlueprintPure, Category = "Health")
float GetHealth() const { return Health; }
UFUNCTION(BlueprintCallable, Category = "Health")
bool ApplyDamage(float Damage);
UPROPERTY(BlueprintAssignable, Category = "Health")
FHealthChangedSignature OnHealthChanged;
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(EditDefaultsOnly, Category = "Health", meta = (ClampMin = "1.0"))
float MaxHealth = 100.0f;
UPROPERTY(VisibleInstanceOnly, Category = "Health")
float Health = 0.0f;
};- Why
- Most developers open a header to find out what the class can do. They should not have to scroll past its internal storage first.
- Costs
- Inline accessors rebuild every dependent translation unit when changed. Keep only trivial accessors inline.
- Instead
- Give subclasses protected accessors when they need controlled access to state. Remove
finalonly when the class has been designed for inheritance.

