我对事件调度器有疑问。我在我的代码中创建了这样的调度程序:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSoundPausedDelegate, bool, isSoundPaused);
UPROPERTY(BlueprintAssignable)
FSoundPausedDelegate AudioPause;
这在蓝图中工作得很好。但是我真的不知道,我怎样才能让它在代码中调用函数?
我想这将是:
AudioPause.AddDynamic(this, &UAudioController::OnDelegateBroadcast);
我应该把它绑定(bind)到什么?这意味着每次我在蓝图中暂停/取消暂停我的音频时广播值,然后根据广播值执行更多代码逻辑。
我的函数是这样的:
void UAudioController::OnDelegateBroadcast(bool SoundPaused)
{
if (SoundPaused == true)
{
SoundPause = true;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("True"));
}
else
{
SoundPause = false;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("False"));
}
}
最佳答案
This is meant to broadcast value every time i pause/unpause my audio in blueprint and then execute more code logic depending on broadcasted value.
所以我假设您尝试:
- 通知游戏中的其他对象,音频播放状态已更改
- 为音频 Controller 中的播放更改提供 C++ 和蓝图实现
我的提议是使用 BlueprintNativeEvent
创建播放更改的内部 UAudioController
实现.基本 C++ 实现将使用您的调度程序将通知传播到其他游戏对象。
简而言之,这是类 .h 文件。
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam( FSoundPausedDelegate, bool, isSoundPaused );
...
//Convenient Play/Pause function for blueprint
UFUNCTION( BlueprintCallable, Category="Example" )
void Play();
UFUNCTION( BlueprintCallable, Category="Example" )
void Pause();
//Implementation for pause state change
UFUNCTION( BlueprintNativeEvent, Category="Example" )
void OnSoundPaused( bool paused );
void OnSoundPaused_Implementation( bool paused );
...
//event dispatcher
UPROPERTY( VisibleAnywhere, BlueprintAssignable )
FSoundPausedDelegate AudioPauseEvent;
方便的函数只需调用实现:
void UAudioController::Play()
{
OnSoundPaused( false );
}
void UAudioController::Pause()
{
OnSoundPaused( true );
}
实现首先定义为 C++ 代码,它还使用 Broadcast
函数触发 AudioPauseEvent
:
void UAudioController::OnSoundPaused_Implementation( bool paused )
{
if( paused == true )
{
SoundPause = true;
GEngine->AddOnScreenDebugMessage( -1, 5.f, FColor::Red, TEXT( "True" ) );
// more logic...
}
else
{
SoundPause = false;
GEngine->AddOnScreenDebugMessage( -1, 5.f, FColor::Red, TEXT( "False" ) );
// more logic...
}
AudioPauseEvent.Broadcast( SoundPause );
}
然后可以在从 UAudioController
派生的 Bluepint 中覆盖 OnSoundPaused
的实现:
AudioPauseEvent
让您可以通知其他游戏对象。下面的示例显示了如何订阅关卡蓝图中的暂停更改:
您还可以从 C++ 中为此事件订阅其他对象(例如 UAudioListener
):
void UAudioListener::StartListeningForAudioPause()
{
// UAudioListener::OnAudioPause should be UFUNCTION
AudioControllerPtr->AudioPauseEvent.AddDynamic( this, &UAudioListener::OnAudioPause );
}
关于c++ - 事件派发器调用代码函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071311/