In this lesson, you will learn how to bridge the gap between abstract performance spikes in your profiler and the specific lines of code in your Blueprints or C++ files that triggered them.
When looking at a massive spike in Unreal Insights or stat unit, you often see a generic "Game Thread" cost. Knowing that the game is slow is useless if you don't know which specific gameplay system is responsible. To fix this, you must correlate the timing data with your logic.
Using Scoped Timers and Custom Markers
The most effective way to map logic to performance is by inserting "markers" into your code. In C++, you can use TRACE_CPUPROFILER_EVENT_SCOPE to wrap specific function blocks. When Insights captures the data, it will create a distinct labeled bar in the timeline, allowing you to see exactly when your code begins and ends.
void AMyCharacter::PerformComplexCalculation()
{
// The label "MyHeavyLogic" will appear in Unreal Insights
TRACE_CPUPROFILER_EVENT_SCOPE(MyHeavyLogic);
// Your gameplay logic here...
for(auto& Actor : LargeActorArray)
{
// Expensive operations
}
}
In Blueprints, you can achieve a similar effect using the Scoped Timer node or, more commonly, by using the Development-only Print String (with custom colors) or by using the Profiler tool within the editor to see if your Blueprint nodes are hogging execution time.
The Strategy
- Locate the Spike: Find the frame in Unreal Insights where the Game Thread time exceeds your budget.
- Drill Down: Expand the call stack. If you see high execution time under a generic
Tickfunction, your code is too broad. - Isolate: Add custom event markers around suspected loops or heavy logic chunks.
- Verify: Re-run your profiling session. If the marker label now appears in your timeline as a significant block, you have successfully correlated the performance cost to that specific gameplay system.
By turning generic CPU spikes into named, identifiable segments, you stop guessing and start targeting the exact logic causing your frame drops.
Key Takeaway: Use scoped event markers to instrument your gameplay code, transforming opaque performance spikes into actionable, named segments within your profiling trace.