Can't AddDynamic to DYNAMIC_MULTICAST_DELEGATE in C++

Can’t bind to DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam. What can be a problem?

My code:

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnSlotUpdateSignature, int, SlotIndex);

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class INVENTORYPLUGIN_API UInventoryComponent : public UActorComponent
{
	GENERATED_BODY()

public:
	UInventoryComponent();
	void AddItem(AItem* Item);
	
	UPROPERTY(BlueprintAssignable, Category = "Listeners")
	FOnSlotUpdateSignature OnSlotUpdateDelegate;
};

From UInventoryComponent I can AddDynamic and Broadcast. But next I have Slot UUserWidget:

UCLASS(Blueprintable, BlueprintType)
class INVENTORYPLUGIN_API UInventorySlot : public UUserWidget
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable, Category = "Inventory")
	void Update();

	virtual void NativeConstruct() override;
	
	UFUNCTION()
	void OnSlotUpdate(int UpdatedSlotIndex);
};

In implementation I can’t do addDynamic:

void UInventorySlot::NativeConstruct()
{
	Super::NativeConstruct();

	const UInventoryComponent* Inventory = GetInventory();

	if (!Inventory)
	{
		return;
	}

	Inventory->OnSlotUpdateDelegate.AddDynamic(this, &UInventorySlot::OnSlotUpdate);
}

Few things that might be it,

The Inventory = const, try removing const.
Delegate is using int. So far I have only used int32 as this is supported by blueprint. Unsure how far int64 support goes with it these days.

1 Like

Replacing const helped me. Thank you.

1 Like

Nice :slight_smile: . Something else not related to your original problem, but you should really make delegate bindings on a widget during OnInitialized. this is because events like Construct and PreConstruct can run multiple time, while OnInitialized runs once.

UUserWidget::Construct | Unreal Engine Documentation

Construct is called every time when you move the widget in the widget hierarchy or remove and re add a widget to the viewport I think.

1 Like