Is a Delegate Necessary?

I want to start with, I don’t fully have my head wrapped around some of the concepts. I thought it was strange that we binded to a delegate in a child class, BP_WinArea. I’ve only used delegates as a listener in another class. I swaped the delegate, OnWinCondition() out for a function, OnWinConditionRegularFunction() and it works fine. Both players stopped input, and I got a Server & Client “You Won!” message In my code the commented blocks are the delegate stuff.


// .h ///////////////

// UDELEGATE()
// DECLARE_DYNAMIC_MULTICAST_DELEGATE(FWinAreaOnWinCondition);

UCLASS()
class TCOOPADVENTURE_API AWinArea : public AActor
{
	GENERATED_BODY()

public:
	
	// /** TODO Doc Variable */
	// UPROPERTY(BlueprintAssignable)
	// FWinAreaOnWinCondition OnWinCondition;

	/** TODO Doc Function */
	UFUNCTION(BlueprintImplementableEvent)
	void OnWinConditionRegularFunction();

// .cpp //////////////////

			if (bWinCondition)
			{
				UE_LOG(LogTemp, Warning, TEXT("Win!"));
				MulticastRPCWin();
			}
		}
	}
}

void AWinArea::MulticastRPCWin_Implementation()
{
	// OnWinCondition.Broadcast();
	OnWinConditionRegularFunction();
}

Using a regular function is a little less code. Don’t have to declare the delegate in the .h file or bind to it in the BP. I would imagine other classes will likely need to know that the game has been won. So I’ll go back to the delegate for extensibility.

Is there any reason specifically that we used a delegate over a regular function?

I don’t really have a clear answer for this. If the delegate was required to trigger functions in other objects then it makes sense to use a delegate.

All I could find is a UE article on the use of delegates which may not be of any help.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms