3D Vampire Survivor Clone

Low-Level Architecture: Data-Oriented Design (SoA)

About the Project

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.

Engine Architecture Highlights

  • Data-Oriented Design (SoA): Bypassed standard Unreal `AActor` overhead by developing a custom Swarm Manager. Entities exist as math within flat C++ arrays (Struct of Arrays), maximizing CPU L1 Cache Locality and enabling SIMD auto-vectorization for hot-loop movement.
  • Batch Instanced Rendering: Decoupled the physics math from the graphics pipeline. The CPU crunches the array math and pushes the transform cache to an Instanced Static Mesh component, rendering 10,000+ entities in a single GPU draw call.
  • Event-Driven Subsystem Architecture: Removed UI `Tick` overhead by developing a custom C++ `UWorldSubsystem`. Health and EXP bars update via multicast delegates when certain events broadcast a hit or level-up payload.
  • O(1) Contiguous Array Deletion: Prevented O(N) memory-shifting frame drops by implementing a "Swap and Pop" deletion algorithm. Dead entities are instantly overwritten by the last active element, maintaining a perfectly dense array for branchless iteration.

Tech Stack

  • Language: C++ & Blueprints
  • Engine: Unreal Engine 5.8
  • Deployment: Windows (PC)
  • Features: Data-Oriented Swarm Architecture, Batched ISM Rendering & Event-Driven UI

                                        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;
                                    
SwarmManager.h Stuct of Array Implementation and UWorldSubsystem definition: Bypassed standard Object-Oriented 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;
                                        }
                                    
O(1) Spatial Hashing & SoA Lookup: The weapon targeting pipeline bypasses standard collision physics entirely. By querying a 3x3 grid of discrete memory buckets, the engine isolates valid targets and uses lightweight integer IDs to index directly into the _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);
                                                }
                                            }
                                        }
                                    
Branchless State Evaluation & Two-Pass Cleanup: By using a Structure of Arrays (SoA) memory layout, the cleanup loop evaluates enemy conditions in a contiguous and branchless first pass. State outcomes are packed into a _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.