虚幻引擎4:未调用C ++委托

Kat*_*nie 6 c++ events delegates callback unreal-engine4

我一直在努力将一些蓝图逻辑转换为C ++。我拥有的一件事是一个按钮。可以在VR中按下该按钮,并具有一个委托,该委托被调用以通知任何已注册的函数按下按钮。这是在AButtonItem.h类中声明委托的方式。

#pragma once
#include "BaseItem.h"
#include "ButtonItem.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FButtonItemPressedSignatrue);

UCLASS()
class AButtonItem : public ABaseItem
{
    GENERATED_BODY()

protected:
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Touch)
    float myMaxButtonPress;

public:

    UPROPERTY(EditAnywhere, Category = Callback)
    FButtonItemPressedSignatrue ButtonItem_OnPressed;
};
Run Code Online (Sandbox Code Playgroud)

然后,按下按钮时将调用委托的广播功能,如下所示:

ButtonItem_OnPressed.Broadcast();
Run Code Online (Sandbox Code Playgroud)

(应该严格调用此函数,因为我有一条调试语句在调用前立即打印。要注意,这在蓝图逻辑中也都可以正常工作。)

这是我尝试向代理注册的地方以及如何声明将被调用的函数:

WeaponMaker.h:

UFUNCTION()
void OnNextBladeButtonPressed();
Run Code Online (Sandbox Code Playgroud)

WeaponMaker.cpp:

void AWeaponMaker::BeginPlay()
{
    Super::BeginPlay();

    TArray<USceneComponent*> weaponMakerComponents;
    this->GetRootComponent()->GetChildrenComponents(true, weaponMakerComponents);

    for (int componentIndex = 0; componentIndex < weaponMakerComponents.Num(); componentIndex++)
    {
        if (weaponMakerComponents[componentIndex]->GetName().Equals("NextBladeButton") == true)
        {
            myNextBladeButton = (AButtonItem*)weaponMakerComponents[componentIndex];
            break;
        }
    }

    if (myNextBladeButton != NULL)
    {
        myNextBladeButton->ButtonItem_OnPressed.AddDynamic(this, &AWeaponMaker::OnNextBladeButtonPressed);
    }

}
Run Code Online (Sandbox Code Playgroud)

我在函数OnNextBladeButtonPressed中放置了一个断点和一条打印语句,因此我应该立即知道它何时起作用,但从未发生。我还从头开始重新创建了蓝图,但仍然没有运气。有时在编译时由于InvocationList无效而导致崩溃,但是我也没有找到关于该问题的太多信息。最重要的是,在应调用OnNextBladeButtonPressed时未调用它。

编辑:这是我在AButtonItem代码中调用广播函数的地方。由于我在控制台中看到UE_LOG输出,因此似乎被调用了:

void AButtonItem::Tick(float deltaTime)
{
    FTransform buttonWorldTransform;
    FVector buttonLocalSpacePos;
    FVector ownerLocalSpacePos;
    FVector localDiff;
    float buttonPressAmount;

    if (myHasStarted == true)
    {
        Super::Tick(deltaTime);

        if (myButtonComponent != NULL)
        {
            if (myPrimaryHand != NULL)
            {
                //Get the world space location of the button.
                buttonWorldTransform = myButtonComponent->GetComponentTransform();

                //Convert the location of the button and the location of the hand to local space.
                buttonLocalSpacePos = buttonWorldTransform.InverseTransformPosition(myInitialOverlapPosition);
                ownerLocalSpacePos = buttonWorldTransform.InverseTransformPosition(myPrimaryHand->GetControllerLocation() + (myPrimaryHand->GetControllerRotation().Vector() * myPrimaryHand->GetReachDistance()));

                //Vector distance between button and hand in local space.
                localDiff = ownerLocalSpacePos - buttonLocalSpacePos;

                //Only interested in the z value difference.
                buttonPressAmount = FMath::Clamp(FMath::Abs(localDiff.Z), 0.0f, myMaxButtonPress);
                localDiff.Set(0.0f, 0.0f, buttonPressAmount);

                //Set the new relative position of button based on the hand and the start button position.
                myButtonComponent->SetRelativeLocation(myButtonInitialPosition - localDiff);

                //UE_LOG(LogTemp, Error, TEXT("buttonPressAmount:%f"), buttonPressAmount);
                if (buttonPressAmount >= myMaxButtonPress)
                {
                    if (myHasBeenTouchedOnce == false)
                    {
                        //Fire button pressed delegate
                        if (ButtonItem_OnPressed.IsBound() == true)
                        {
                            ButtonItem_OnPressed.Broadcast();
                            AsyncTask(ENamedThreads::GameThread, [=]()
                            {
                                ButtonItem_OnPressed.Broadcast();
                            });
                        }

                        myHasBeenTouchedOnce = true;
                        myButtonComponent->SetScalarParameterValueOnMaterials("State", 1.0f);
                        Super::VibrateTouchingHands(EVibrationType::VE_TOUCH);
                    }
                }
            }
            else
            {
                //Slowly reset the button position back to the initial position when not being touched.
                FVector newPosition = FMath::VInterpTo(myButtonComponent->GetRelativeTransform().GetLocation(), myButtonInitialPosition, deltaTime, 10.0f);
                myButtonComponent->SetRelativeLocation(newPosition);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*SFT 2

有时在编译时,由于 InvocableList 无效,我会崩溃,但我也没有找到有关该问题的太多信息。底线是,OnNextBladeButtonPressed 没有在应该被调用的时候被调用。

我在问题的代码中没有看到任何问题。在我看来,问题可能出在不同的位置。我怀疑《AWeaponMaker》在播出时就被删除了。