Hi, i met a problem about delegate in UE C++, I have an ActorComponent class named as CardDealerComponent, i defined a one parameter multicast delegate in this class.
CardDealerComponent.h
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPlayCard,APWCard*, Card);
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class CARDGAME_API UPWCardDealerComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UPWCardDealerComponent();
UPROPERTY(BlueprintAssignable)
FOnDrawCard OnPlayCard;
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction) override;
UFUNCTION(BlueprintCallable)
bool ApplyPlayCard(APWCard* Card);
};
CardDealerComponent.cpp
bool UPWCardDealerComponent::ApplyPlayCard(APWCard* Card)
{
OnPlayCard.Broadcast(Card);
UKismetSystemLibrary::PrintString(GetWorld(), TEXT("BroadCast Play"));
return true;
}
And i have a Card class, it has CardDealerComponent. When on clicked event happened, card class call CardDealerComponent’s ApplyPlayCard(APWCard* Card) function which broadcasts OnPlayCard event.
Card.h
UCLASS()
class CARDGAME_API APWCard : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APWCard();
UPROPERTY(EditAnywhere, BlueprintReadOnly)
UStaticMeshComponent* MeshComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UPWCardDealerComponent* CardDealerComponent;
bool bInHand;
protected:
UFUNCTION()
void OnMouseClicked(UPrimitiveComponent* TouchedComponent, FKey ButtonPressed);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
Card.cpp
APWCard::APWCard()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>("CardMesh");
RootComponent = MeshComponent;
CardDealerComponent = CreateDefaultSubobject<UPWCardDealerComponent>("CardDealerComponent");
bInHand = false;
}
void APWCard::BeginPlay()
{
Super::BeginPlay();
MeshComponent->OnClicked.AddDynamic(this, &APWCard::OnMouseClicked);
}
void APWCard::OnMouseClicked(UPrimitiveComponent* TouchedComponent, FKey ButtonPressed)
{
if(bInHand)
{
CardDealerComponent->ApplyPlayCard(this);
}
}
Lastly i also have a Hand class, which also has CardDealerComponent, and i bind a UFUNCTION() to CarDDealerComponent->OnPlayCard() by CardDealerComponent->AddDynamic(). I suppose when i clicked a card, the card class will broadcast with itself as parameter.Then the Hand class will recieve this broadcast and execute function to add this card into hand. But the function added by AddDynamic won’t execute.
Hand.h
class CARDGAME_API APWHand : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APWH
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable)
void PlayCard(APWCard* Card);
}
Hand.cpp
APWHand::APWHand()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
CardDealerComponent = CreateDefaultSubobject<UPWCardDealerComponent>("CardDealerComponent");
}
void APWHand::PostInitializeComponents()
{
Super::PostInitializeComponents();
CardDealerComponent->OnPlayCard.AddDynamic(this, &APWHand::PlayCard);
}
// Called when the game starts or when spawned
void APWHand::BeginPlay()
{
Super::BeginPlay();
APWDeck* DeckActor = Cast<APWDeck>(UGameplayStatics::GetActorOfClass(GetWorld(), Deck));
DeckActor->CardDealerComponent->OnDrawCard.AddDynamic(this, &APWHand::OnDrawCard);
}
// Called every frame
void APWHand::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void APWHand::PlayCard(APWCard* Card)
{
UKismetSystemLibrary::PrintString(GetiWorld(), TEXT("PlayCard");
}
Anyone could help me i will really apprieciated.
