# Check if there is any dialog playing

**URL:** https://qa.fmod.com/t/check-if-there-is-any-dialog-playing/21781
**Category:** Unity
**Tags:** unity, csharp
**Created:** [June 19, 2024, 4:07pm UTC](https://qa.fmod.com/t/check-if-there-is-any-dialog-playing/21781 "2024-06-19T16:07:50Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![UltMate\_Potato](https://avatars.discourse-cdn.com/v4/letter/u/e8c25b/32.png) [@UltMate\_Potato](https://qa.fmod.com/u/UltMate_Potato)
#### Post date: [June 19, 2024, 4:07pm UTC](https://qa.fmod.com/t/check-if-there-is-any-dialog-playing/21781/1 "2024-06-19T16:07:50Z")

</div>

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:

```auto
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;
            }
            
        }

```

---

<div class="post-metadata">

### Author: ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)
#### Post date: [June 24, 2024, 2:04am UTC](https://qa.fmod.com/t/check-if-there-is-any-dialog-playing/21781/2 "2024-06-24T02:04:02Z")

</div>

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`](https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#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](https://www.fmod.com/docs/2.02/unity/user-guide.html#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](https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#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](https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_setuserdata), either by itself, or contained in a struct or class - see our [Timeline Callbacks](https://www.fmod.com/docs/2.02/unity/examples-timeline-callbacks.html) and [Programmer Sounds](https://www.fmod.com/docs/2.02/unity/examples-programmer-sounds.html) scripting examples for examples on how to do this
