Functions
reusable blocks of code that perform a specific task
Key Characteristics of UE5 Functions:
-
Code Reusability:
Functions encapsulate a set of operations that can be called multiple times from different parts of your Blueprint or C++ code, eliminating the need to duplicate logic.
-
Input Parameters and Output Return Values:
Functions can take inputs (parameters) to customize their behavior and can return output values, allowing them to provide results to the calling code.
-
Local Variables:
Functions can have local variables, which exist only within the scope of that function and are destroyed once the function finishes executing.
-
Immediate Execution:
Functions execute their internal logic immediately upon being called. They do not support latent actions like delays or timelines, which involve a sense of timing.
-
Organization and Readability:
Functions help break down complex logic into smaller, manageable units, making your Blueprints or C++ code cleaner and easier to understand.
-
Pure Functions:
Functions can be marked as "Pure," meaning they do not modify the state of the object they are called on and always return the same output for the same inputs. Pure functions appear as green nodes in Blueprints without execution pins.
-
Overriding in Child Classes:
Functions defined in parent classes can be overridden in child classes, allowing for specialized behavior while maintaining a common interface.
-
Blueprint Function Libraries:
Functions can be grouped into Blueprint Function Libraries, making them accessible across various Blueprints in your project without needing a specific object context.
Examples of Function Use Cases:
-
Calculating a character's damage based on various stats.
-
Setting the visibility and collision of a mesh component.
-
Performing complex mathematical operations within materials (Material Functions).
-
Handling user interface updates based on game state changes.

Things to Remember
Common Best Practice
Prioritize Clear Naming Conventions
Use descriptive names for functions and their parameters. For C++, follow Epic's C++ Coding Standard (e.g., U prefix for UObject classes, A for AActor classes, b for booleans). Encapsulate reusable logic within functions instead of duplicating code. This promotes efficiency and simplifies future modifications.
Embrace Pure Functions for Calculations and Data Retrieval
Use pure functions for operations that do not modify the object's state and always return the same output for the same input. Be mindful of performance implications: if a pure function is computationally expensive and its output is used multiple times, consider storing the result in a local variable to avoid redundant calculations.
Design Functions for Reusability
Aim for functions that are primarily driven by input parameters and produce output, minimizing reliance on internal blueprint variables (unless they are constants). This makes functions more versatile and easier to integrate into different contexts. Consider creating Blueprint Function Libraries for widely applicable utility functions.
Employ Getter Functions
Use getter functions to access internal variables instead of directly referencing them. This provides a layer of abstraction, allowing for potential validation or manipulation logic within the getter without affecting external code.
Optimize Performance
Avoid excessive work within functions called frequently (e.g., in Tick events). If a function is performance-critical, consider optimizing its implementation or reducing its call frequency. Be judicious with casting, as it can be expensive and lead to performance issues if overused, especially in long chains. Interfaces often provide a more efficient alternative for interaction systems.