Implementing UInterface functions in Blueprint

One of the key advantages of UInterface in Unreal is the ability for users to implement UInterface functions in the editor. This means the interface can be implemented strictly in Blueprint without needing any C++ code, which is helpful to designers.

How to do it...

  1. Create a new UInterface called AttackAvoider.
  2. Add the following function declaration to the header:
    UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category = AttackAvoider)
    voidAttackIncoming(AActor* AttackActor);
  3. Create a new Blueprint Class within the Editor:
    How to do it...
  4. Base the class on Actor:
    How to do it...
  5. Open Class Settings:
    How to do it...
  6. Click on the drop-down menu for Implement Interface, and select AttackAvoider:
    How to do it...
  7. Compile your blueprint:
    How to do it...
  8. Right-click in the Event 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:
    How to do it...
  9. Drag 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.
    How to do it...
  10. You have now implemented a UInterface function within Blueprint.

How it works...

  1. The UINTERFACE/IInterface are created in exactly the same way that we see in other recipes in this chapter.
  2. When we add a function to the interface, however, we use a new UFUNCTION specifier: BlueprintImplementableEvent.
  3. BlueprintImplementableEvent tells the Unreal Header Tool to generate code that creates an empty stub function that can be implemented by Blueprint. We do not need to provide a default C++ implementation for the function.
  4. We implement the interface inside Blueprint, which exposes the function for us in a way that allows us to define its implementation in Blueprint.
  5. The autogenerated code created by the header tool forwards the calls to the UInterface function to our Blueprint implementation.

See also

  • The following recipe shows you how to define a default implementation for your UInterface function in C++, then optionally override it in Blueprint if necessary
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.248.159