The standard delegates used so far in this chapter are essentially a function pointer—they allow you to call one particular function on one particular object instance. Multicast delegates are a collection of function pointers, each potentially on different objects, that will all be invoked when the delegate is broadcast.
This recipe assumes you have followed the initial recipe in the chapter, as it shows you how to create TriggerVolume
that is used to broadcast the multicast delegate.
GameMode
header:DECLARE_MULTICAST_DELEGATE(FMulticastDelegateSignature)
Actor
class called MulticastDelegateListener
. Add the following to the declaration:UFUNCTION() void ToggleLight(); UFUNCTION() virtual void EndPlay(constEEndPlayReason::Type EndPlayReason) override; UPROPERTY() UPointLightComponent* PointLight; FDelegateHandleMyDelegateHandle;
PointLight = CreateDefaultSubobject<UPointLightComponent>("PointLight"); RootComponent = PointLight;
MulticastDelegateListener.cpp
file, add #include "UE4CookbookGameMode.h"
between your project's include
file and the MulticastDelegateListener
header include. Inside the MulticastDelegateListener::BeginPlay
implementation, add the following:Super::BeginPlay(); UWorld* TheWorld = GetWorld(); if (TheWorld != nullptr) { AGameMode* GameMode = UGameplayStatics::GetGameMode(TheWorld); AUE4CookbookGameMode * MyGameMode = Cast<AUE4CookbookGameMode>(GameMode); if (MyGameMode != nullptr) { MyDelegateHandle = MyGameMode->MyMulticastDelegate.AddUObject(this, &AMulticastDelegateListener::ToggleLight); } }
ToggleLight
:void AMulticastDelegateListener::ToggleLight() { PointLight->ToggleVisibility(); }
EndPlay
overridden function:void AMulticastDelegateListener::EndPlay(constEEndPlayReason::Type EndPlayReason) { Super::EndPlay(EndPlayReason); UWorld* TheWorld = GetWorld(); if (TheWorld != nullptr) { AGameMode* GameMode = UGameplayStatics::GetGameMode(TheWorld); AUE4CookbookGameMode * MyGameMode = Cast<AUE4CookbookGameMode>(GameMode); if (MyGameMode != nullptr) { MyGameMode->MyMulticastDelegate.Remove(MyDelegateHandle); } } }
TriggerVolume::NotifyActorBeginOverlap()
:MyGameMode->MyMulticastDelegate.Broadcast();
GameMode
in your level to be our cookbook game mode, then drag four or five instances of the MulticastDelegateListener
into the scene.TriggerVolume
to see all the MulticastDelegateListener
toggle their light's visibility.Listener
class is very similar to our original DelegateListener
. The primary difference is that we need to store a reference to our delegate instance in FDelegateHandle
.FDelegateHandle
as a parameter to Remove()
.Broadcast()
function is the multicast equivalent of ExecuteIfBound()
. Unlike standard delegates, there is no need to check if the delegate is bound either in advance or with a call like ExecuteIfBound
. Broadcast()
is safe to run no matter how many functions are bound, or even if none are.GameMode
.TriggerVolume
overlaps a player, it broadcasts the delegate, and each Listener is notified causing them to toggle the visibility of their associated point light.3.138.125.139