How to bind a component function from an actor delegate

I have a weapon component, and in order to make it so I can easily switch it out with other weapon component without having to have the player directly equip items, I am trying to have the component drive all the major attack functionality.

To make sure I can basically equip the weapon to anything based on a particular class, meaning both AI and player (i.e. AI when called through behaviour tree or something, player when called through input), I have setup a number of delegates in a base character class (from which I have extended both enemy and the player character), and then try to bind these within the weapon component.

However, I am not getting any reaction. While the IsBound() returns true, the function within the component is not being called. Here is the functionality in question:

Base Character .h

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FStartAttackSignature);

UPROPERTY(BlueprintAssignable, Category = "Attack")
	FStartAttackSignature OnStartAttack;

Base character .cpp

void ABaseCharacter:StartAttack()
{
	if(OnStartAttack.IsBound())
	{
		OnStartAttack.Broadcast();
	}
}

Weapon component .h

	virtual void BeginPlay() override;

	void OnStartAttack();

Weapon component .cpp

void UWeaponComponent::BeginPlay()
{
	WeaponOwner = Cast<ABaseCharacter>(GetOwner());
	if (WeaponOwner)
	{
		WeaponOwner->OnStartAttack.AddDynamic(this, &UWeaponComponent::OnStartAttack);
	}

	Super::BeginPlay();
}

void UWeaponComponent::OnStartAttack()
{

}

Unbelievable, right after I post this thread I suddenly start getting in-engine errors through the log, complaining about how my bound functions are not UFUNCTIONs.

So I stick the UFUNCTION on the OnStartAttack function and now it works. So yeah, the problem is solved. Best thing is that when I got that error, I remembered I had this error before a few years ago.

1 Like