【Unreal Engine】Blueprint Variables 101: Proper Use of Class Variables vs Local Variables

Created: 2025-12-12

Guidelines for deciding whether to create Blueprint variables as Class Variables or Local Variables. Understanding scope differences and proper usage to prevent bugs.

Overview of Class Variables and Local Variables

There are two main ways to define variables in Blueprint:

  1. Class Variables (Member Variables): Variables created in Blueprint's "Variables" panel.
  2. Local Variables: Variables created only inside functions or event graphs.

Not understanding these differences and defining everything as Class Variables leads to bloated Blueprints and increased bug potential. This article explains the roles and proper usage principles for these two variable types.

Characteristics of Class Variables and Local Variables

Let's clearly understand each variable type's characteristics and roles.

Class Variables (Member Variables)

CharacteristicDescription
Definition LocationBlueprint editor's "Variables" panel
ScopeEntire Blueprint instance
LifecycleValues retained from Blueprint instance creation to destruction
AccessAccessible and modifiable from all functions, events, and graphs within the Blueprint. Also accessible from external Blueprints (when set to Public)
Primary UseObject state, configuration values, persistent references

Class Variables are used to define "what state the Blueprint (object) is currently in." For example, player character HP, current movement speed, targeted enemy reference—data that should be retained throughout the object's lifecycle and referenced/updated by multiple processes.

Local Variables

CharacteristicDescription
Definition LocationWithin a Function, from the "Local Variables" section of My Blueprint panel
ScopeOnly within the execution block of the function where the variable was created
LifecycleCreated at function start, destroyed at function end
AccessOnly accessible and modifiable within the defining function. No external access whatsoever
Primary UseTemporary calculation results, intermediate data, reusing specific node outputs

Important Note: Local Variables Are Defined Within Functions

In UE5, Local Variables can only be defined within Functions. You cannot directly create local variables in Event Graphs. If you need to temporarily hold values in an Event Graph, split processing into Functions or use Class Variables.

Local Variables are used only for temporarily holding values during specific processing. For example, "calculate A and B, store result in C, then use C to calculate D"—ideal for holding intermediate data that completes within one function or event. They're released from memory when processing ends, keeping things very clean.


Principles for Proper Usage

Variable usage becomes clear when based on the "minimize scope" principle.

Best Practice Principle: If a variable is only used within one function/event, always define it as a Local Variable. Only define as Class Variables those things that need to be shared across multiple functions/events or persisted, like object state or configuration values.

Class Variables Usage Example

Consider managing player character HP. HP is "object state" that gets referenced and updated in multiple places—damage events, healing events, UI updates, etc.

Variables to define as Class Variables:

  • CurrentHP (Integer/Float): Current health
  • MaxHP (Integer/Float): Maximum health
  • IsDead (Boolean): Death state

Blueprint Example (Class Variable Usage):

// Event Receive Damage (when damage is received)
// 1. Get Class Variable CurrentHP
// 2. Subtract damage amount from CurrentHP
// 3. Set result to CurrentHP (update Class Variable)
// 4. Check if CurrentHP is 0 or below
// 5. If 0 or below, Set IsDead to True (update another Class Variable)

In this case, CurrentHP is referenced not only by the damage event but also by healing functions and UI update events, making it appropriate to define as a Class Variable.

Local Variables Usage Example

Consider performing complex calculations within a function and temporarily using the results.

Variables to define as Local Variables:

  • CalculatedDistance (Float): Distance calculation result to target
  • FinalDamageMultiplier (Float): Final damage multiplier after applying multiple buffs/debuffs

Blueprint Example (Local Variable Usage):

// Function: CalculateFinalDamage
// 1. Get Base Damage (Class Variable)
// 2. Get Buff Multiplier (Class Variable)
// 3. Set Base Damage * Buff Multiplier result to Local Variable: IntermediateDamage
// 4. Set IntermediateDamage * Critical Hit Multiplier result to Local Variable: FinalDamage
// 5. Return FinalDamage as the function's Return Value

In this example, IntermediateDamage is only needed while CalculateFinalDamage is executing. Since it's unnecessary after the function ends, defining it as a Local Variable keeps the Blueprint clean and completely eliminates effects on other processing.


Common Mistakes and Debugging Tips

Making Everything Class Variables

The most common beginner mistake is defining everything as Class Variables—temporary calculation results, references only used within a function, everything.

Problems:

  • Reduced Blueprint Readability: Variable list grows long, truly important state variables get buried.
  • Difficult Debugging: Values change unexpectedly in unintended places, making bug source identification hard ("Why did this value change?").
  • Memory Waste: Not destroyed after processing ends, occupying memory as long as the instance lives (minor but should be avoided in principle).

Debugging Efficiency Tips

Actively using Local Variables dramatically eases debugging.

  • Scope Guarantee: Local Variables only exist during function/event execution, so changes are guaranteed to occur only within that function. If there's a bug, investigating within that function is sufficient—narrowing the scope.
  • Node Organization: When complex calculation nodes continue, temporarily storing results in Local Variables prevents wire tangling and organizes node graphs.

Importance of Local Variables in Loops

When calculating totals in For Loops etc., not using Local Variables can cause unintended results.

// Bad Example: Using Class Variable to calculate totals in loop
// Function: CalculateTotalScore
// 1. For Each Loop (ScoreArray)
// 2. Add Array Element to TotalScore (Class Variable)
// Problem: Calling function twice doubles the total because previous value remains!

// Good Example: Using Local Variable to calculate totals in loop
// Function: CalculateTotalScore
// 1. Initialize Local Variable: RunningTotal to 0
// 2. For Each Loop (ScoreArray)
// 3. Add Array Element to RunningTotal
// 4. Return RunningTotal after loop ends
// Benefit: RunningTotal starts at 0 each function call, always giving accurate totals

Key Point

Class Variables retain values as long as the instance lives, so using them as loop accumulator variables causes bugs where previous call values remain. Use Local Variables for temporary loop accumulation.


Variable Selection Checklist

Proper use of Class Variables and Local Variables is a crucial factor in Blueprint quality. Use this checklist as your criteria when creating variables.

QuestionClass Variable (Member)Local Variable
Q1. Does it hold object state?YES (HP, state flags, settings)NO
Q2. Is it referenced/updated by multiple functions or events?YESNO
Q3. Does value need to be retained after processing ends?YESNO
Q4. Is it temporary data used only within one function/event?NOYES (intermediate calculations, temporary references)
Q5. Is it OK to discard the value when processing ends?NOYES

Conclusion: To keep Blueprints clean and prevent bugs, having a clear distinction of "State uses Class Variable, Process uses Local Variable" is the first step as an Unreal Engine developer.