Custom Actors without components don't have a location, and can't be attached to other Actors. Without a root Component, an Actor doesn't have a base transform, and so it has no location. Most Actors, therefore, require at least one Component to be useful.
We can create custom Actors through composition—adding a number of components to our Actor
, where each component provides some of the functionality required.
This recipe will use the Actor
class created in the Creating a custom Actor in C++ recipe.
public
section:UPROPERTY() UStaticMeshComponent* Mesh;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>("BaseMeshComponent");
UCLASS() class UE4COOKBOOK_API AMyFirstActor : public AActor { GENERATED_BODY() public: AMyFirstActor(); UPROPERTY() UStaticMeshComponent* Mesh; }; #include "UE4Cookbook.h" #include "MyFirstActor.h" AMyFirstActor::AMyFirstActor() { PrimaryActorTick.bCanEverTick = true; Mesh = CreateDefaultSubobject<UStaticMeshComponent>("BaseMeshComponent"); }
StaticMeshComponent
that we added.UPROPERTY macro
we added to the class declaration is a pointer to hold the component we are using as a subobject of our Actor
.UPROPERTY() UStaticMeshComponent* Mesh;
UPROPERTY()
macro ensures that the object declared in the pointer is considered to be referenced, and won't be garbage-collected (that is, deleted) out from under us, leaving the pointer dangling.Actor
Component subclasses would work. Note the asterisk is connected to the variable type in accordance with Epic's style guide.template
function, template<class TReturnType> TReturnType* CreateDefaultSubobject(FName SubobjectName, bool bTransient = false)
.3.146.37.150