- Create a new UInterface called AttackAvoider:
- Add the following function declaration to the header:
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "AttackAvoider.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UAttackAvoider : public UInterface
{
GENERATED_BODY()
};
class CHAPTER_08_API IAttackAvoider
{
GENERATED_BODY()
// Add interface functions to this class. This is the class
// that will be inherited to implement this interface.
public:
UFUNCTION(BlueprintImplementableEvent, BlueprintCallable,
Category = AttackAvoider)
void AttackIncoming(AActor* AttackActor);
};
- Compile your project. From the Content Browser, open the Content folder and then create a new Blueprint Class within the Editor by selecting Add New | Blueprint Class:
- Base the class on Actor:
- Name the blueprint AvoiderBlueprint and then double-click on it to open the Blueprint Editor. From there, open Class Settings:
- Under the Details tab, click on the drop-down menu that says Add for Implemented Interfaces, and select AttackAvoider:
- Compile your Blueprint:
- Open the Event Graph by clicking on the Event Graph tab and then right-click within the graph and type event attack. Within the Context Sensitive menu, you should see Event Attack Incoming. Select it to place an event node in your graph:
- Drag this out from the execution pin on the new node and release. Type print string into the Context Sensitive menu to add a Print String node:
Selecting the Print String node
You have now implemented a UInterface function within Blueprint.
- To see the event in action, drag the pin to the right of the Event BeginPlay event and call an Attack Incoming event:
- Drag and drop an instance of your Blueprint class into the level and play the game:
If all went well, you should see the default message from Print String, or whatever you posted to happen when the event should happen!