【发布时间】:2017-09-20 23:14:04
【问题描述】:
我在绑定 OnAudioFinished 委托时遇到问题。
搜索了一段时间,但还没有找到好的答案。我关注了这个answer!
我的代码编译时完全没有任何错误,但是当我的项目加载时它会因以下错误而崩溃:
UE4Editor_!TBaseDynamicMulticastDelegate<FWeakObjectPtr,void>::__Internal_AddDynamic<UAudioController>() [d:\path\delegates\delegatesignatureimpl.inl:1140]
UE4Editor_Project!UAudioController::UAudioController() [d:\path\private\audiocontroller.cpp:17]
UE4Editor_Project!InternalConstructor<UAudioController>()
我所理解的是构造函数会破坏我的引擎,但我不知道为什么会这样。这是我负责此绑定的代码。
.h
static UAudioComponent* AudioComponent;
public:
UAudioController();
void SoundFinished();
.cpp
UAudioController::UAudioController()
{
AudioComponent->OnAudioFinished.AddDynamic(this, &UAudioController::SoundFinished);
}
void UAudioController::SoundFinished()
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("Audio Finished trigger"));
}
【问题讨论】:
-
您的调试器将成为此类问题的最佳朋友。也就是说,我的猜测是,当您调用 AddDynamic 时,AudioComponent 尚未初始化。
-
还要确保你的
SoundFinished函数是UFUNCTION。 -
请检查我编辑的答案。使用
NewObject<...>()(正如您在下面的评论中提到的)绝对不是使用组件的正确方法。
标签: c++ constructor delegates unreal-engine4