Actors and Components🔗
Actors and components are two of the fundamental gameplay types in unreal code.
Creating a new actor or component type in script is as simple as creating a new script file and adding a class that inherits from an actor type:
Note: The script plugin automatically sets the most useful class flags for any script classes, adding a
UCLASS()specifier is not necessary in script, but can still optionally be used to configure additional class settings.
Unlike in C++, script classes do not make use of their constructors for creating components.
To add a default component to the actor, use the
DefaultComponent specifier for them.
Default components are automatically created on the actor when it is spawned.
The following class will have two components on it when placed. A scene component at the root, and the custom
UMyComponent we declared before:
Likewise, the default attachment hierarchy is specified in
UPROPERTY specifiers, rather than set up in a constructor. Use the
Attach = and
AttachSocket = specifiers.
If an explicit attachment is not specified, the component will be attached to the actor's root.
Note: You can explicitly note which component should be the default root component with the
RootComponentspecifier. If you do not add this specifier, the first component to be created will become the root.
To assign default values to properties on the actor's components, you can use
Working with Components🔗
If you have an actor and want to find a component of a type on it, use
If no component of the specified type exists, this will return
UMyComponentClass::GetOrCreate() to retrieve a potential existing component, or create it if one does not exist already:
Adding New Components🔗
Creating a new component works similarly by calling
Specifying a component name is optional, if none is specified one will be automatically generated.
Tip: If you have a dynamic
UClassfor a component class, you can also use the generic functions on actors for these operations by using
Helpers for spawning actors of a specific type are also available:
Spawning a Blueprinted Actor🔗
It is often needed to dynamically spawn an actor blueprint, rather than a script actor baseclass.
To do this, use a
TSubclassOf<> property to reference the blueprint, and use the global
An example of a spawner actor that can be configured to spawn any blueprint of an example actor:
Actor construction script can be added by overriding the
ConstructionScript() blueprint event.
From construction scripts, you can create new components and override properties like normal.
For example, an actor that creates a variable amount of meshes inside it based on its settings in the level could look like this:
Getting All Actors or Components🔗
To get all components of a particular type that are on an actor, use
Actor.GetComponentsByClass() and pass in the array.
This function takes a
? parameter, and will determine which component type to look for by the type of array you pass in.
For example, to get all static meshes on an actor:
Similarly, to get all actors of a particular type that are currently in the world, use the
GetAllActorsOfClass() global function, and pass in an array of the type of actor you want:
Note: Getting all actors of a class requires iterating all actors in the level, and should not be used from performance-sensitive contexts. That is, try running it once and storing the value rather than using it every tick.
Unreal provides a mechanism for overriding one of a parent actor class' default components to use a child component class instead of the one specified on the parent actor.
In script, this can be accessed by using the
Note: Override components are similar to using
ObjectInitializer.SetDefaultSubobjectClass()in a C++ constructor.