How to do it...

  1. Create a new UInterface called AttackAvoider:

  1. 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);
};
  1. 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:

  1. Base the class on Actor:

  1. Name the blueprint AvoiderBlueprint and then double-click on it to open the Blueprint Editor. From there, open Class Settings:

  1. Under the Details tab, click on the drop-down menu that says Add for Implemented Interfaces, and select AttackAvoider:

  1. Compile your Blueprint:

  1. 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:

  1. 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.

  1. To see the event in action, drag the pin to the right of the Event BeginPlay event and call an Attack Incoming event:

  1. 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!