Check if there is any dialog playing

hey guys!
im new to fmod unity and i have a question.
i have a general_speaker script used for my npcs that have dialog that plays their dialog.

is there a way for me to check if any other dialog is playing when the script tries to play a new dialog, so that we can either return if we cant interrupt that dialog, or stop the event instance of that previous dialog if we are allowed to interrupt?

here is my code as of now, which seems to work but after i added this unity crashes sometimes:

void PlayDialog(AI_Dialog targetDialog, bool forceInterruption = false)
        {
            //--CHECK IF THERE ARE ANY ECENTS PLAYING AND HANDLE INTERRUPTION OF THOSE EVENTS--
            //check if an event is playing
            if (PlayingInstance.isValid())
            {
                PlayingInstance.getPlaybackState(out PLAYBACK_STATE state);
                if (state != PLAYBACK_STATE.STOPPED)
                {
                    //but if we CAN interrupt. stop any playing voice lines
                    if (forceInterruption)
                        PlayingInstance.stop(STOP_MODE.IMMEDIATE);
                    else
                        return;
                }
            }
            //----------------------------------------------------------------------------------
            FMOD.Studio.EVENT_CALLBACK dialogueCallback = new FMOD.Studio.EVENT_CALLBACK(SentenceEventCallback);

            PlayingInstance = FMODUnity.RuntimeManager.CreateInstance(targetDialog.audio);
            FMODUnity.RuntimeManager.AttachInstanceToGameObject(PlayingInstance, audioTransform);

            PlayingInstance.setCallback(dialogueCallback);
            //Start the sound
            PlayingInstance.start();
            PlayingInstance.release();
        }

[AOT.MonoPInvokeCallback(typeof(EVENT_CALLBACK))]
        FMOD.RESULT SentenceEventCallback(EVENT_CALLBACK_TYPE type, System.IntPtr instancePtr, System.IntPtr parameterPtr)
        {
            try
            {
                //var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES));
                FMOD.Sound sound = new FMOD.Sound(parameterPtr);
                sound.getName(out string eventName, 1024);

                switch (type)
                {
                    case EVENT_CALLBACK_TYPE.STARTED:
                        {
                            playingASound = true;
                            break;
                        }
                    case EVENT_CALLBACK_TYPE.STOPPED:
                        {
                            playingASound = false;
                            break;
                        }
                }
                return FMOD.RESULT.OK;
            }
            catch (System.Exception)
            {

                throw;
            }
            
        }

What you’re doing in your code snippet is roughly how to go about checking if an event instance is playing. You can either query Studio::EventInstance::getPlaybackState, or use an event callback to receive information on the event’s state as it happens.

As for the crash, it’s likely due to issues with/in the event callback. I can see a few potential issues:

  1. Callbacks need to follow a few restrictions, outlined in the Callbacks of our Unity user guide - in this case, your method needs to be static.

  2. The data passed to the argument parameterPtr by FMOD depends on what FMOD_STUDIO_EVENT_CALLBACK_TYPE has been fired - you should check the callback type before doing anything with the callback argument.

  3. I’m unsure how the variable playingASound is defined, but if it is being directly referenced from the callback, I would recommend that it be static. If the variable needs to be non-static, it should be passed to the callback via event user data, either by itself, or contained in a struct or class - see our Timeline Callbacks and Programmer Sounds scripting examples for examples on how to do this