Implementing a simple interaction system with UInterfaces

This recipe will show you how to combine a number of other recipes in this chapter to demonstrate a simple interaction system, and a door with an interactable doorbell to cause the door to open.

How to do it...

  1. Create a new interface, Interactable.
  2. Add the following functions to the IInteractable class declaration:
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category=Interactable)
    boolCanInteract();
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Interactable)
    voidPerformInteract();
  3. Create default implementations for both functions in the implementation file:
    boolIInteractable::CanInteract_Implementation() { return true; } voidIInteractable::PerformInteract_Implementation() ...

Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.