Search code examples
c++event-handlingunreal-engine4

Event dispatcher calling code function


I have a question about event dispatchers. I've created dispatcher in my code like this:

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSoundPausedDelegate, bool, isSoundPaused);

UPROPERTY(BlueprintAssignable)
        FSoundPausedDelegate AudioPause;

This works perfectly fine in blueprints. However I don't really know, how can i make it to call function in code?

I guess it's going to be something with:

AudioPause.AddDynamic(this, &UAudioController::OnDelegateBroadcast);

What shall I bind it to? 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.

This is how my function looks like:

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

Solution

  • 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.

    So I assume you try to:

    1. Inform other objects in game, that audio playback state is changed
    2. Provide C++ and Blueprint implementation for playback change in your audio controller

    My proposition is to create inner UAudioController implementation of playback change using BlueprintNativeEvent. Base C++ implementation will use your dispatcher to propagate notification to other game objects.

    This is class .h file in short.

        
        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;
    

    Convenient functions simply call implementation:

    void    UAudioController::Play()
    {
        OnSoundPaused( false );
    }
    
    
    void    UAudioController::Pause()
    {
        OnSoundPaused( true );
    }
    

    Implementation is defined first as C++ code, which also fire AudioPauseEvent with Broadcast function:

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

    Implementation of OnSoundPaused could be then overridden in Bluepint derived from UAudioController:

    enter image description here

    Having AudioPauseEvent let you to notify other game objects. Example below show how to subscribe for pause change in level blueprint:

    enter image description here

    You could also subscribe other objects (for example UAudioListener) from C++ for this event:

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