-
Notifications
You must be signed in to change notification settings - Fork 68
Actors and components
This covers the basics of using AActor and UActorComponent
The properties AActor.PrimaryActorTick
/ UActorComponent.PrimaryComponentTick
are used to control the ticking functionality of actors and components. These properties are of type FTickFunction
which is a struct (essentially a pointer to the C++ FTickFunction
).
Enabling AActor tick:
public override void Initialize(FObjectInitializer initializer)
{
base.Initialize(initializer);
PrimaryActorTick.SetStartWithTickEnabled(true);
PrimaryActorTick.SetCanEverTick(true);
}
Enabling UActorComponent tick:
public override void Initialize(FObjectInitializer initializer)
{
base.Initialize(initializer);
PrimaryComponentTick.SetStartWithTickEnabled(true);
PrimaryComponentTick.SetCanEverTick(true);
}
It's best to enable tick in the Initialize
function as the ticker is registered with the engine tickers just before the C# BeginPlay
function is called. If you need to enable the ticker in BeginPlay
you may need to manually register the ticker via FTickFunction.RegisterTickFunction
.
This is an example of adding a UStaticMeshComponent
as the RootComponent
and then attaching a UTextRendererComponent
.
[UClass]
class ATestActor : AActor
{
[UProperty, EditAnywhere, BlueprintReadWrite]
public UStaticMeshComponent Mesh { get; set; }
[UProperty, EditAnywhere, BlueprintReadWrite]
public UTextRenderComponent TextRenderer { get; set; }
public override void Initialize(FObjectInitializer initializer)
{
UStaticMesh cubeMesh = ConstructorHelpers.FObjectFinder<UStaticMesh>.Find("StaticMesh'/Engine/BasicShapes/Cube.Cube'");
if (cubeMesh != null)
{
Mesh = initializer.CreateDefaultSubobject<UStaticMeshComponent>(this, (FName)"Mesh");
Mesh.SetStaticMesh(cubeMesh);
RootComponent = Mesh;
TextRenderer = initializer.CreateDefaultSubobject<UTextRenderComponent>(this, (FName)"TextRenderer");
TextRenderer.SetupAttachment(Mesh);
using (FText text = FText.FromString("Text component test"))
{
TextRenderer.SetText(text);
}
}
}
}
This is an example of spawning an AActor and adding a UStaticMeshComponent
at runtime in the BeginPlay
function of a AGameMode
defined type.
[UClass]
class ATestGameMode : AGameMode
{
protected override void BeginPlay()
{
base.BeginPlay();
UStaticMesh cubeMesh = ConstructorHelpers.FObjectFinder<UStaticMesh>.Find("StaticMesh'/Engine/BasicShapes/Cube.Cube'");
if (cubeMesh != null)
{
AActor actor = World.SpawnActor<AActor>(default(FVector), default(FRotator));
UStaticMeshComponent meshComponent = NewObject<UStaticMeshComponent>(actor, (FName)"Mesh");
meshComponent.SetStaticMesh(cubeMesh);
meshComponent.RegisterComponent();
actor.RootComponent = meshComponent;
}
}
}
By default components wont show up in the "Add Component" list in the editor. To enable this add the [BlueprintSpawnableComponent]
attribute.
[UClass, BlueprintSpawnableComponent]
class UTestComponent : UActorComponent
{
}
[UClass]
class AInputTestCharacter : ACharacter
{
protected override void SetupPlayerInputComponent(UInputComponent playerInputComponent)
{
base.SetupPlayerInputComponent(playerInputComponent);
// Set-up "Trigger" in the input bindings in the editor.
playerInputComponent.BindAction("Trigger", EInputEventType.Pressed, OnTrigger);
}
// Must be marked as a [UFunction] so that the engine can call this function when the button is pressed
[UFunction]
private void OnTrigger()
{
PrintString("OnTrigger", FLinearColor.Red);
}
}