Now that we have the sphere constructed properly via the Editor, let's include the functionality that gets the AHybridSphere
saying hello and goodbye. We are going to be creating this functionality a little differently this time. We are again going to be utilizing the delegate system detailed previously, but this time we are going to be writing functions that have a base functionality in code, which then may be overridden in a Blueprint if deemed necessary. This means we can create C++ functions that provide a default functionality set that will execute if no override has been defined in Blueprint. These functions are declared via the UFUNCTION()
macro and the BlueprintNativeEvent
specifier. Navigate to HybridSphere.h
and add the following code to the AHybridSphere
class definition underneath the virtual Tick
function declaration:
// On Overlap implementation UFUNCTION(BlueprintNativeEvent) void MyOnBeginOverlap(AActor* OtherActor); void MyOnBeginOverlap_Implementation(AActor* OtherActor); // On End Overlap implementation UFUNCTION(BlueprintNativeEvent) void MyOnEndOverlap(AActor* OtherActor); void MyOnEndOverlap_Implementation(AActor* OtherActor);
As you can see, we have created the same MyOnBeginOverlap()
function, however this time we have specified that it is a BlueprintNativeEvent
. That means that this event can be defined in any Blueprint abstractions. However, if it is not, look at this code class for a base implementation. You will also notice that we have included the function MyOnBeginOverlap_Implementation()
. This is simply so that when we define the base implementation in our .cpp
, the UBT knows where to look. For every BlueprintNativeEvent
that you define, you must include an accompanying Implementation version of the function with the following format [FunctionName]_Implementation(Same parameter list)
. Without this the compiler will throw an error.
Alright, let's add the definition for our new overlap functions to the HybridSphere.cpp. We will be adding the definition to the Implementation versions of the collision functions. Navigate, to HybridSphere.cpp
and add the following code:
void AHybridSphere::MyOnBeginOverlap_Implementation(AActor* OtherActor) { FString outputString; outputString = "Hello From C++!"; Text->SetText(FText::FromString(outputString)); } void AHybridSphere::MyOnEndOverlap_Implementation(AActor* OtherActor) { Text->SetText(NSLOCTEXT("AnyNS", "Any", "Goodbye From C++")); }
As you can see, it is very similar to the code we used in our AHelloSphere
except this time we have changed the greeting to Hello From C++
. This is so it is clear whether we are using the C++ base implementation of the BlueprintNativeEvent
or our Blueprint Extension.
The last thing we need to do is provide these functions to the on begin and end overlap delegates! Add the following code to the constructor definition under our component creation code:
OnActorBeginOverlap.AddDynamic(this, &AHybridSphere::MyOnBeginOverlap); OnActorEndOverlap.AddDynamic(this, &AHybridSphere::MyOnEndOverlap);
It is important that you provide the original BlueprintNativeEvent
function not the Implementation version of the function as the BlueprintNativeEvent
will be calling our base implementation.
Ok, let's hot compile this code and check that it works in the level! When the compilation is complete, ensure there is a FixedBP
object in your level, press play, and navigate towards the HybridSphere
to be greeted from C++!
Ok, now let's demonstrate the power of Blueprint Native Events. We are going to duplicate our FixedBP
to create a new separate blueprint within which we can then specify our BlueprintNativeEvent
functionality. Specifying this functionality will override our C++ base implementation and it will no longer be used when an overlap begins or ends.
Do this now by right-clicking FixedBP
in the Content browser and selecting Duplicate, this will create a blueprint that is, by default, called FixedBP2
. You can change this if you like or keep it as is. You will notice that all of the changes we made to the components in the Blueprint editor for the original FixedBP
have been preserved.
Open the FixedBP2
Blueprint and navigate to the Event Graph. Here we are going to be summoning two event nodes, Event MyOnBeginOverlap
and Event MyOnEndOverlap
. By summoning these two nodes we are effectively stating that we wish to override the C++ base implementation of these two events and use the node arrangement following these event nodes instead of the C++ base implementation. What we are going to do is set the text on our Text (inherited) component. Now it will be made obvious as to why we specified our UTextRenderComponent
in AHybridSphere
with BlueprintReadOnly
.
Ensure that you have checked Show Inherited variables and, under the Variables section of the MyBlueprint panel, you will see a new category titled Components. Expand this now and you will see a reference to our UTextRenderComponent
titled Text. Drag a reference to this component as a get
node into the event graph and summon a Set Text
node from it. Then summon a make literal text
node, enter Hello From Blueprint
! for the input to this literal node, and plug the output into the set text
node. Connect the Set Text
node to the Event MyOnBeginOverlap
node. Duplicate the set text functionality and connect these new nodes to the Event MyEndBeginOverlap
node. In the copied make literal
text node, populate the input with Goodbye From Blueprint!
. You should have an arrangement that looks similar to this:
Now compile the Blueprint and drag an instance of it into the level next to our original FixedBP
object. Ensure that there is enough space between the two of them so that we may interact with one but not the other, this should be easy as our pawn is currently flying.
Run into both in turn and you will see an output like this:
As you can see, the FixedBP2
object on the right has deferred to using the Blueprint implementation of the event where our original FixedBP
object is still using the code definition, as there is no Blueprint specification provided. Keep in mind that both objects inherit from the same base class!
3.145.191.214