Chapter 5. Handling Events and Delegates

Unreal uses events for notifying classes about things that happen in the game world in an efficient manner. Events and delegates are useful to ensure that these notifications can be issued in a way which minimizes class coupling, and allows arbitrary classes to subscribe to be notified.

We will cover the following recipes in this chapter:

  • Handling events implemented via virtual functions
  • Creating a delegate that is bound to a UFUNCTION
  • Unregistering a delegate
  • Creating a delegate that takes input parameters
  • Passing payload data with a delegate binding
  • Creating a multicast delegate
  • Creating a custom Event
  • Creating a Time of Day handler
  • Creating a respawning pickup for an First Person Shooter

Handling events implemented via virtual functions

Some Actor and Component classes provided with Unreal include event handlers in the form of virtual functions. This recipe will show you how to customize those handlers by overriding the virtual function in question.

How to do it...

  1. Create an empty Actor in the Editor. Call it MyTriggerVolume.
  2. Add the following code to the class header:
    UPROPERTY()
    UBoxComponent* TriggerZone;
    
    UFUNCTION()
    virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
    UFUNCTION()
    virtual void NotifyActorEndOverlap(AActor* OtherActor) override;
  3. Add the implementation for the preceding functions to the cpp file:
    void AMyTriggerVolume::NotifyActorBeginOverlap(AActor* OtherActor)
    {
      GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, FString::Printf(TEXT("%s entered me"),*(OtherActor->GetName())));
    }
    
    void AMyTriggerVolume::NotifyActorEndOverlap(AActor* OtherActor)
    {
      GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, FString::Printf(TEXT("%s left me"), *(OtherActor->GetName())));
    }
  4. Compile your project, and place an instance of MyTriggerActor into the level. Verify that overlap/touch events are handled by walking into the volume, and seeing the output printed to the screen:
    How to do it...

How it works...

  1. As always, we first declare a UPROPERTY to hold a reference to our component subobject. We then create two UFUNCTION declarations. These are marked as virtual and override so that the compiler understands we want to replace the parent implementation, and that our function implementations can be replaced in turn.
  2. In the implementation of the functions, we use FString::printf to create an FString from some preset text, and substitute some data parameters.
  3. Note that the FString OtherActor->GetName() returns, and is dereferenced using the * operator before being passed into FString::Format. Not doing this results in an error.
  4. This FString is then passed to a global engine function, AddOnScreenDebugMessage.
  5. The first argument of -1 tells the engine that duplicate strings are allowed, the second parameter is the length of time the message should be displayed for in seconds, the third argument is the color, and the fourth is the actual string to print itself.
  6. Now when a component of our actor overlaps something else, its UpdateOverlaps function will call NotifyActorBeginOverlap, and the virtual function dispatch will call our custom implementation.
..................Content has been hidden....................

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