While marking classes as BlueprintType
or Blueprintable
allows us to pass instances of the class around in Blueprint, or to subclass the type with a Blueprint class, those specifiers don't actually say anything about member functions or variables, and if they should be exposed to Blueprint.
This recipe shows you how to mark a function so that it can be called within Blueprint graphs.
Actor
class using the editor. Call the actor SlidingDoor
.UPROPERTY
to the new class:UFUNCTION(BlueprintCallable, Category = Door) void Open(); UPROPERTY() bool IsOpen; UPROPERTY() FVector TargetLocation;
.cpp
file:ASlidingDoor::ASlidingDoor() :Super() { auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'")); if (MeshAsset.Object != nullptr) { GetStaticMeshComponent()->SetStaticMesh(MeshAsset.Object); GetStaticMeshComponent()->bGenerateOverlapEvents = true; } GetStaticMeshComponent()->SetMobility(EComponentMobility::Movable); GetStaticMeshComponent()->SetWorldScale3D(FVector(0.3, 2, 3)); SetActorEnableCollision(true); IsOpen = false; PrimaryActorTick.bStartWithTickEnabled = true; PrimaryActorTick.bCanEverTick = true; } void ASlidingDoor::Open() { TargetLocation = ActorToWorld().TransformPositionNoScale(FVector(0, 0, 200)); IsOpen = true; } void ASlidingDoor::Tick(float DeltaSeconds) { if (IsOpen) { SetActorLocation(FMath::Lerp(GetActorLocation(), TargetLocation, 0.05)); } }
SlidingDoor
instance selected, then open the Level blueprint. Right-click on the empty canvas, and expand Call function on Sliding Door 1.Open
function.BeginPlay
to the white arrow on the Open
node, as seen in the following screenshot:Open
is invoked on your door instance.Tick
actor function so that we can perform some behavior on every frame.IsOpen
to a known good value of false
and enable actor ticking by using bCanEverTick
and bStartWithTickEnabled
.Open
function, we calculate the target location relative to the door's starting position.IsOpen
Boolean from false
to true
.IsOpen
Boolean is true
, inside the Tick
function, the door tries to move itself towards the target location using SetActorLocation
and Lerp
to interpolate between the current location and the destination.3.15.151.21