Another way that C++ can be more tightly integrated with Blueprint is the creation of functions that can have Blueprint implementations in native code. This allows for a programmer to specify an event, and invoke it, without needing to know anything about the implementation. The class can then be subclassed in Blueprint, and another member of the production team can implement a handler for the event without ever having to go near a line of C++.
StaticMeshActor
class called Spotter
.virtual void Tick( float DeltaSeconds ) override; UFUNCTION(BlueprintImplementableEvent) void OnPlayerSpotted(APawn* Player);
PrimaryActorTick.bCanEverTick = true; auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cone.Cone'")); if (MeshAsset.Object != nullptr) { GetStaticMeshComponent()->SetStaticMesh(MeshAsset.Object); GetStaticMeshComponent()->bGenerateOverlapEvents = true; } GetStaticMeshComponent()->SetMobility(EComponentMobility::Movable); GetStaticMeshComponent()->SetRelativeRotation(FRotator(90, 0, 0));
Tick
function:Super::Tick( DeltaTime ); auto EndLocation = GetActorLocation() + ActorToWorld().TransformVector(FVector(0,0,-200)); FHitResult HitResult; GetWorld()->SweepSingleByChannel(HitResult, GetActorLocation(), EndLocation, FQuat::Identity, ECC_Camera, FCollisionShape::MakeSphere(25), FCollisionQueryParams("Spot", true, this)); APawn* SpottedPlayer = Cast<APawn>(HitResult.Actor.Get()); if (SpottedPlayer!= nullptr) { OnPlayerSpotted(SpottedPlayer); } DrawDebugLine(GetWorld(), GetActorLocation(), EndLocation, FColor::Red);
Spotter
class in Content Browser, then left-click and drag a copy out into the game world.Actor
is performing. However, nothing will happen, because we haven't implemented our OnPlayerSpotted
event.Spotter
.Spotter
in Content Browser, and select Create Blueprint class based on Spotter. Name the class BPSpotter
.Print String
node so that it is linked to the event.Spotter
is using now prints a string to the screen.Spotter
object, we load one of the basic primitives, a cone, into our Static Mesh Component as a visual representation.Tick
function, we get the actor's location, and then find a point 200 units away from the actor along its local X axis. We call the parent class implementation of Tick
using Super::
to ensure that any other tick functionality is preserved despite our override.Actor
, then using that to transform a vector specifying the position.SweepSingleByChannel
function.Actor
into a pawn.OnPlayerSpotted
, and the user-defined Blueprint code executes.18.223.209.59