Search code examples
delegatesunreal-engine4c++-winrtunreal-blueprint

Unreal Engine:How to solve the problem of using BroadCast() of Delegate in C++/WinRT Async function?


I am currently creating Voice Recognition by using C++/WinRT and binding it into Unreal Engine. Because I wanna let my blueprint widget to show the words when it got recognized, I created a DECLARE_MULTICAST_DELEGATE in my C++/WinRT file and use a function to BroadCast() it to notify widget when words are recognized. Here is the code

VoiceRecog.h VoiceRecog.h

VoiceRecog.cpp VoiceRecog.cpp-1

VoiceRecog.cpp VoiceRecog.cpp-2

BP_WinRTVoiceRec(Create from VoiceReocg.cpp) BP_WinRTVoiceRec(Create from VoiceReocg.cpp)

The problem is when it got recognized it crashed, and I checked the log it said enter image description here

It seems like it did enter the function and broadcast, but stuck at the bind event… I’ve testted this logic by trigger the ActivateDIspatcher() function with normal blueprint Actor, it works fine…so I believe it definitely should be the problem of this Async function.

Does anyone know how to solve this? Thank you!


Solution

  • The problem is that you are firing the event from a thread which is not the game thread, and then trying to play audio from that thread. The audio system does not allow you to play sound from any other thread except the game thread.

    It is generally good practice to make sure blueprint events are fired from the game thread only because you don't know what a consumer might try to do in that event, and many game framework elements can only be accessed form the game thread.

    You can instead use the AsyncTask construct to fire the event on the game thread.

    #include "Async/Async.h"
    

    AsyncTask(ENamedThreads::GameThread, [=]() {
        OnRecognized.Broadcast();
    });