gpt4 book ai didi

c++ - 虚幻引擎 4 : C++ Delegate not being called

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:33:11 24 4
gpt4 key购买 nike

我一直致力于将一些蓝图逻辑转换为 C++。我拥有的东西之一是一个按钮。该按钮可以在 VR 中按下,并且有一个委托(delegate)被调用以通知任何已注册的函数按钮按下发生了。以下是在 AButtonItem.h 类中声明委托(delegate)的方式。

#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;
};

当按下按钮时,代理的广播功能将被调用,如下所示:

ButtonItem_OnPressed.Broadcast();

(这个函数应该被挑衅地调用,因为我有一个在调用之前打印的调试语句。同样重要的是要注意,当它是蓝图逻辑时,这一切都是有效的。)

这是我尝试向委托(delegate)注册的地方以及我如何声明将被调用的函数:

WeaponMaker.h:

UFUNCTION()
void OnNextBladeButtonPressed();

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);
}

}

我在函数 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);
}
}
}
}

最佳答案

首先:

UPROPERTY(EditAnywhere, Category = Callback)
FButtonItemPressedSignatrue ButtonItem_OnPressed;

这应该是:

UPROPERTY(BlueprintAssignable, Category = Callback)
FButtonItemPressedSignatrue ButtonItem_OnPressed;

为了方便。

其次,出于多种原因,可能会在执行开始播放之前调用 tick 函数。如果游戏还没有开始,您的事件将不会被广播。因此,为了避免只在您的滴答函数中添加一个检查。

if(bHasBegunPlay)
{
// .. your logics ...
}

关于c++ - 虚幻引擎 4 : C++ Delegate not being called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56821209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com