事件派发器调用代码函数
Event dispatcher calling code function
我对事件调度程序有疑问。我在我的代码中创建了这样的调度程序:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSoundPausedDelegate, bool, isSoundPaused);
UPROPERTY(BlueprintAssignable)
FSoundPausedDelegate AudioPause;
这在蓝图中工作得很好。但是我真的不知道,我怎样才能让它在代码中调用函数?
我想这会是:
AudioPause.AddDynamic(this, &UAudioController::OnDelegateBroadcast);
我应该绑定什么?这意味着每次我 pause/unpause 我的音频在蓝图中广播值,然后根据广播值执行更多代码逻辑。
我的函数是这样的:
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"));
}
}
您需要另一个名为 BlueprintImplementableEvent 的 UPROPERTY,这使您可以定义一个可以被 Blueprint 覆盖的代码函数。参见 https://answers.unrealengine.com/questions/38960/blueprintimplementableevent-in-level-blueprint.html
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.
所以我假设您尝试:
- 通知游戏中的其他对象,音频播放状态已更改
- 为音频控制器中的播放更改提供 C++ 和蓝图实现
我的建议是使用 BlueprintNativeEvent
创建内部 UAudioController
回放更改实现。基本 C++ 实现将使用您的调度程序将通知传播到其他游戏对象。
这是 class .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++ 代码,它还触发 AudioPauseEvent
和 Broadcast
函数:
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 );
}
OnSoundPaused
的实现可以在派生自 UAudioController
的 Bluepint 中被覆盖:
有AudioPauseEvent
让你通知其他游戏对象。下面的示例显示了如何订阅级别蓝图中的暂停更改:
您还可以从 C++ 订阅此事件的其他对象(例如 UAudioListener
):
void UAudioListener::StartListeningForAudioPause()
{
// UAudioListener::OnAudioPause should be UFUNCTION
AudioControllerPtr->AudioPauseEvent.AddDynamic( this, &UAudioListener::OnAudioPause );
}
我对事件调度程序有疑问。我在我的代码中创建了这样的调度程序:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSoundPausedDelegate, bool, isSoundPaused);
UPROPERTY(BlueprintAssignable)
FSoundPausedDelegate AudioPause;
这在蓝图中工作得很好。但是我真的不知道,我怎样才能让它在代码中调用函数?
我想这会是:
AudioPause.AddDynamic(this, &UAudioController::OnDelegateBroadcast);
我应该绑定什么?这意味着每次我 pause/unpause 我的音频在蓝图中广播值,然后根据广播值执行更多代码逻辑。
我的函数是这样的:
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"));
}
}
您需要另一个名为 BlueprintImplementableEvent 的 UPROPERTY,这使您可以定义一个可以被 Blueprint 覆盖的代码函数。参见 https://answers.unrealengine.com/questions/38960/blueprintimplementableevent-in-level-blueprint.html
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.
所以我假设您尝试:
- 通知游戏中的其他对象,音频播放状态已更改
- 为音频控制器中的播放更改提供 C++ 和蓝图实现
我的建议是使用 BlueprintNativeEvent
创建内部 UAudioController
回放更改实现。基本 C++ 实现将使用您的调度程序将通知传播到其他游戏对象。
这是 class .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++ 代码,它还触发 AudioPauseEvent
和 Broadcast
函数:
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 );
}
OnSoundPaused
的实现可以在派生自 UAudioController
的 Bluepint 中被覆盖:
有AudioPauseEvent
让你通知其他游戏对象。下面的示例显示了如何订阅级别蓝图中的暂停更改:
您还可以从 C++ 订阅此事件的其他对象(例如 UAudioListener
):
void UAudioListener::StartListeningForAudioPause()
{
// UAudioListener::OnAudioPause should be UFUNCTION
AudioControllerPtr->AudioPauseEvent.AddDynamic( this, &UAudioListener::OnAudioPause );
}