Function Declaration
Functions can exist in two basic forms: a regular C++ function or a UFunction
. Both C++ functions and UFunctions
are declared in the .h
class header file. C++ functions are declared using the standard C++ syntax for declaring a function. UFunctions
use a specialized syntax that allows additional information about the function to be specified in the declaration through the use of Function Specifiers.
UFUNCTION([specifier, specifier, ...], [meta(key=value, key=value, ...)])
ReturnType FunctionName([Parameter, Parameter, ...])
UFunctions
behave the same as C++ functions in that they have a C++ implementation, can be called from within C++ code, and can contain calls to other C++ functions or UFunctions
within their function bodies. UFunctions
differ in a few areas, however. For instance, they can be called or overridden from within the Blueprint Visual Scripting system. Any UFunction
declared using the BlueprintCallable
, BlueprintImplementableEvent
, or BlueprintPure
specifiers (see Function Specifiers below for more details) will be exposed to Blueprints. UFunctions
can also be assigned as delegates within the default properties of a class. This technique is commonly used to bind input actions or axes to functions in Input
classes. UFunctions
are also used as replication callbacks; meaning the UFunction
will be called when the variable it is associated with changes and is replicated across the network. UFunctions
are also the only type of function that can be declared as an exec function allowing them to be called by the player from the in-game console during play.
Function Specifiers
When declaring functions, Function Specifiers can be added to the declaration to control how the function behaves with various aspects of the Engine and Editor.
Function Specifier |
Effect |
---|---|
|
This function will only execute from Blueprint code if running on a machine with network authority (a server, dedicated server, or single-player game). |
|
The function can be executed in a Blueprint or Level Blueprint graph. |
|
This function is cosmetic and will not run on dedicated servers. |
|
This function will be used as the accessor for a Blueprint-exposed property. This specifier implies |
|
The function can be implemented in a Blueprint or Level Blueprint graph. |
|
Indicates that the function should not be exposed to the end user. |
|
This function is designed to be overridden by a Blueprint, but also has a default native implementation. Declares an additional function named the same as the main function, but with |
|
The function does not affect the owning object in any way and can be executed in a Blueprint or Level Blueprint graph. |
|
This function will be used as the mutator for a Blueprint-exposed property. This specifier implies |
|
This function can be called in the Editor on selected instances via a button in the Details Panel. |
|
Specifies the category of the function when displayed in Blueprint editing tools. Define nested categories using the |
|
The function is only executed on the client that owns the Object on which the function is called. Declares an additional function named the same as the main function, but with |
|
The UnrealHeaderTool code generator will not produce a thunk for this function; it is up to the user to provide one. |
|
The function can be executed from the in-game console. Exec commands only function when declared within certain classes. |
|
The function is executed both locally on the server, and replicated to all clients, regardless of the Actor's |
|
The function is replicated over the network, and is guaranteed to arrive regardless of bandwidth or network errors. Only valid when used in conjunction with |
|
This function cannot be overridden in subclasses. The |
|
This function is an RPC (Remote Procedure Call) service request. |
|
This function is an RPC service response. |
|
The function is only executed on the server. Declares an additional function named the same as the main function, but with |
|
The function is replicated over the network but can fail due to bandwidth limitations or network errors. Only valid when used in conjunction with |
|
Declares an additional function named the same as the main function, but with |
Metadata Specifiers
The usage of Metadata Specifiers differs between regular classes, functions, and interfaces.
Function Meta Tag |
Effect |
---|---|
|
The comma-separated list of parameters will show up as advanced pins (requiring UI expansion). |
|
Replace |
|
Indicates that a |
|
When |
|
The listed parameters, although passed by reference, will have an automatically-created default if their pins are left disconnected. This is a convenience feature for Blueprints. |
|
Used only by static |
|
This function is an internal implementation detail, used to implement another function or node. It is never directly exposed in a Blueprint Graph. |
|
This function can only be called on the owning Object in a Blueprint. It cannot be called on another instance. |
|
Used for |
|
Indicates that a |
|
Indicates that a |
|
The listed parameters are all treated as wildcards. This specifier requires the To declare a custom |
|
For |
|
Any Blueprint references to this function will cause compilation warnings telling the user that the function is deprecated. You can add to the deprecation warning message (e.g. to provide instructions on replacing the deprecated function) using the |
|
If the function is deprecated, this message will be added to the standard deprecation warning when trying to compile a Blueprint that uses it. |
|
Functions marked as |
|
The name of this node in a Blueprint will be replaced with the value provided here, instead of the code-generated name. |
|
For |
|
For |
|
Hides the "self" pin, which is used to indicate the object on which the function is being called. The "self" pin is automatically hidden on |
|
Similar to |
|
Specifies a set of keywords that can be used when searching for this function, such as when placing a node to call the function in a Blueprint Graph. |
|
Indicates a latent action. Latent actions have one parameter of type |
|
For Latent |
|
For |
|
For |
|
Only valid in Blueprint Function Libraries. This function will be treated as an exception to the owning class's general |
|
A short tooltip that is used in some contexts where the full tooltip might be overwhelming, such as the parent class picker dialog. |
|
Overrides the automatically generated tooltip from code comments. |
|
This function is not safe to call during Actor construction. |
|
Used by |
Function Parameter Specifiers
Out |
Declares the parameter as being passed by reference allowing it to be modified by the function. |
Optional |
With the optional keyword, you can make certain function parameters optional, as a convenience to the caller. The values for optional parameters which the caller does not specify depend on the function. For example, the |
Function Implementations
Functions declared in the class header file are given definitions in the class source file.
Delegates
Delegates allow you to call member functions on C++ objects in a generic, yet type-safe way. Using delegates, you can dynamically bind to a member function of an arbitrary object, then call functions on the object, even if the caller does not know the object's type.
It is perfectly safe to copy delegate objects. Delegates can be passed around by value but this is generally not recommended since they do have to allocate memory on the heap. You should always pass delegates by reference when possible.
Both single-cast and multi-cast delegates are supported, as well as "dynamic" delegates which can be safely serialized to disk.
Single-cast
See the Delegates page for reference and usage information.
Timers
Timers schedule actions to be performed after a delay, or at a over a period of time. For example, you may want to make the player invulnerable after obtaining a power-up item, and then restore vulnerability after 10 seconds. Or, you may want to apply damage once per second while the player moves through a room filled with toxic gas. Each of these can be achieved through the use of timers.
See the Gameplay Timers page for reference and usage information.