I’m trying to bind a delegate inside an overlap event where with a door.
- Player walks in Door’s proximity
-
OverlapEvent
triggers - User gets a reference to the Door (
DoorReference
member is subclass ofATriggerBox
) - BindUObject on
DoorReference
delegateOnPuzzleSuccess
- Solve the puzzle
- If correct
ExecuteIfBound
the user function.
All good till step 5. Afterwards no delegate is called. Upon checking the delegate if isBound
I get true immediately after step 4. but false before step 6.
Do I have to bind Delegates inside BeginPlay()
?
DoorTrigger.h
.
.
.
DECLARE_DELEGATE_OneParam(FPuzzleSuccess, AInteractableItem *);
UCLASS()
class SOME_API ADoorTrigger : public ATriggerBox
{
public:
FPuzzleSuccess OnPuzzleSuccess;
UFUNCTION(BlueprintCallable)
void HandleSuccessCompletion();
.
.
.
}
DoorTrigger.cpp
void ADoorTrigger::HandleSuccessCompletion()
{
if (OnSuccessCompletion.IsBound()) {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("it is bound"));
}
OnSuccessCompletion.ExecuteIfBound(this->SuccessPrize);
}
void ADoorTrigger::onComboSubmission(bool isCorrect)
{
if (isCorrect) {
// do stuff
HandleSuccessCompletion(); // call the delegate
}
else {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Nope"));
}
}
Player.cpp
...
void APlayer::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor->IsA(ADoorTrigger::StaticClass())) {
DoorReference = Cast<ADoorTrigger>(OtherActor);
DoorReference->OnSuccessCompletion.BindUObject(this, &APlayer::OnPuzzleSuccess); // OnPuzzleSuccess has the UFUNCTION MACRO
}
}
void APlayer::OnPuzzleSuccess(AInteractableItem* Prize)
{
if (DoorReference) {
InventoryComponent->AddItem(Prize);
}
}