c++ - 事件派发器调用代码函数

标签 c++ event-handling unreal-engine4

我对事件调度器有疑问。我在我的代码中创建了这样的调度程序:

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.

所以我假设您尝试:

  1. 通知游戏中的其他对象,音频播放状态已更改
  2. 为音频 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 的实现:

enter image description here

AudioPauseEvent 让您可以通知其他游戏对象。下面的示例显示了如何订阅关卡蓝图中的暂停更改:

enter image description here

您还可以从 C++ 中为此事件订阅其他对象(例如 UAudioListener):

void UAudioListener::StartListeningForAudioPause()
{
    // UAudioListener::OnAudioPause should be UFUNCTION
    AudioControllerPtr->AudioPauseEvent.AddDynamic( this, &UAudioListener::OnAudioPause );
}

关于c++ - 事件派发器调用代码函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071311/

相关文章:

c++ - 我可以在我的网站上展示我使用虚幻引擎制作的游戏吗?

c++ - 带彩虹括号的 Vim 折叠

c++ - 适用于事件管理的设计模式

在 Java 中创建按钮事件时发生 Java 运行时错误

c++ - 作为指针的 const 成员函数的返回值的属性是什么?

c++ - 在 C++ 中访问蓝图类组件的正确方法是什么

c++ - cpp文件中的纯虚接口(interface)实现

c++ - 检查使用 SDL2 播放的音乐

c++ - 如何使用多线程 C++ 在循环中启动多个操作

jQuery 使用事件处理程序删除元素