Low-Level Architecture: Data-Oriented Design (SoA)
A deep dive into Data-Oriented Design and Structure of Arrays to develop a custom Entity Component System using C++ in Unreal Engine, to render a 10000 entity swarm without separate actors maintaining 80fps.
private:
class UProgressionMessageSubsystem* ProgressRadio = nullptr;
class UCombatMessageSubsystem* CombatRadio = nullptr;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
UInstancedStaticMeshComponent* ISMComponent;
TArray _cleanupFlags;
TArray _InstanceTransforms;
TArray _EnemyDirection;
TArray _EnemyPosition;
TArray _EnemyVelocity;
TArray _EnemyScale;
TArray _speed;
TArray _damage;
TArray _health;
int _aliveEnemiesCount = 0;
int _enemyCount = 10000;
AActor overhead through a Struct of Arrays (SoA) memory layout. Storing entity transforms, velocity, and combat stats in flat, contiguous TArray blocks using CPU L1 Cache Locality and enables branchless SIMD auto-vectorization. Active entities are maintained via an array (tracking _aliveEnemiesCount for O(1) Swap-and-Pop deletion), pushing batched transform updates to a single ISM component while safely routing decoupled game events through custom C++ World Subsystems.
int ASwarmManager::GetClosestEnemyIndex(const FVector& Location)
{
TRACE_CPUPROFILER_EVENT_SCOPE(Swarm_GetClosestEnemyIndex);
int closestEnemyIndex = CheckClosestGrid(Location);
if (closestEnemyIndex != -1) { return closestEnemyIndex; }
int minDistance = INT_MAX;
for (int i = 0; i < _aliveEnemiesCount; i++)
{
int distance = FVector::DistSquared(_EnemyPosition[i], Location);
if (distance < minDistance)
{
minDistance = distance;
closestEnemyIndex = i;
}
}
return closestEnemyIndex;
}
int ASwarmManager::CheckClosestGrid(const FVector& Location)
{
int index = CalculateHitZone(Location);
if (spatialGrid[index].Num() == 0) { return -1; }
int closestEnemyIndex = -1;
int minDistance = INT_MAX;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
int pos = (index + i) + (j * hitZoneDepth);
if (pos < 0 || pos >= spatialGrid.Num()) { continue; }
for (int k = 0; k < spatialGrid[pos].Num(); k++)
{
int enemyIndex = spatialGrid[pos][k];
int distance = FVector::DistSquared(_EnemyPosition[enemyIndex], Location);
if (distance < minDistance)
{
minDistance = distance;
closestEnemyIndex = enemyIndex;
}
}
}
}
return closestEnemyIndex;
}
_EnemyPosition flat array. This localized memory access, combined with squared-distance math, removes L1 cache misses and trigonometry.
void ASwarmManager::CleanupTickFrame()
{
FVector* playerPos = CombatRadio->GetPlayerPosition();
const float AttackRangeSqrt = 10000.0f;
for (int i = 0; i < _aliveEnemiesCount; i++)
{
bool _healthZero = _health[i] <= 0;
bool _inAttackRange = FVector::DistSquared(_EnemyPosition[i], *playerPos) < AttackRangeSqrt;
bool isAttacking = !_healthZero && _inAttackRange;
_cleanupFlags[i] = _healthZero + (isAttacking * 2);
}
for (int i = _aliveEnemiesCount - 1; i >= 0; i--)
{
if (_cleanupFlags[i] == 1)
{
KillEnemy(i);
}
else if (_cleanupFlags[i] == 2)
{
CombatRadio->OnPlayerHit.Broadcast(_damage[i]);
KillEnemy(i);
}
}
}
_cleanupFlags array. A secondary reverse-iteration pass executes the heavier callbacks and removal logic, this two part system allows the mathematical calculations using SIMD to optimise performance.