# How to handle dialogue with programmer callbacks?

**URL:** <https://qa.fmod.com/t/how-to-handle-dialogue-with-programmer-callbacks/18618>\
**Category:** Unity\
**Tags:** unity, csharp\
**Created:** [April 5, 2022, 4:42am UTC](https://qa.fmod.com/t/how-to-handle-dialogue-with-programmer-callbacks/18618 "2022-04-05T04:42:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![amtm](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@amtm](https://qa.fmod.com/u/amtm)\
**Post date:** [April 5, 2022, 4:42am UTC](https://qa.fmod.com/t/how-to-handle-dialogue-with-programmer-callbacks/18618/1 "2022-04-05T04:42:19Z")

</div>

Hello,

I’m using the following guide to setup my programmer instrument and link it to an Audio Tree.  
[https://fmod.com/resources/documentation-unity?version=2.02&page=examples-programmer-sounds.html](https://fmod.com/resources/documentation-unity?version=2.02&page=examples-programmer-sounds.html)

I’m struggling to understand how to manage the dialogue using callbacks. I searched for online resources to no avail and also tried random ideas to understand how to check for the callback type but I’m getting nowhere.

What I would like to achieve is - when an audio file from the programmer ends playing, I would like to play another sound, when that one ends, play another one. I’ve implemented an alternative using the multi instrument but with that I have no clear indication when a sound ends.

I’m assuming that I have to check for FMOD.Studio.EVENT\_CALLBACK\_TYPE in the Update() method and if it is equal to FMOD.Studio.EVENT\_CALLBACK\_TYPE.STOPPED, to trigger my next audio sample?

Also, I don’t understand where this callback is stored. Is it in

> var dialogueInstance = FMODUnity.RuntimeManager.CreateInstance(“event:/World01/Prog”);

Or is it here -

> dialogueCallback = new FMOD.Studio.EVENT\_CALLBACK(DialogueEventCallback);

I saw that there is a setCallback() method that can be applied to dialogueInstance but I couldn’t understand how to get the callback.

Thanks!

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [April 7, 2022, 6:17am UTC](https://qa.fmod.com/t/how-to-handle-dialogue-with-programmer-callbacks/18618/2 "2022-04-07T06:17:13Z")

</div>

> [@amtm](#):
>
> I’m assuming that I have to check for FMOD.Studio.EVENT\_CALLBACK\_TYPE in the Update() method and if it is equal to FMOD.Studio.EVENT\_CALLBACK\_TYPE.STOPPED, to trigger my next audio sample?

Correct, that is how you would do it.

> [@amtm](#):
>
> Also, I don’t understand where this callback is stored.

The second one is the callback. An event instance is a playable instance of the event that you setup in FMOD Studio. A callback is a function with a specific signature that will be called at a time of the event’s choosing. You need to create this callback function yourself and assign the callback to the event instance with `setCallback`.

In the example this is the callback function:

```auto
static FMOD.RESULT DialogueEventCallback(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr)

```

You need to create an instance of this callback:

```auto
dialogueCallback = new FMOD.Studio.EVENT_CALLBACK(DialogueEventCallback);

```

And link it to the event instance:

```auto
dialogueInstance.setCallback(dialogueCallback);

```

Then you will be able to do things at different point in the event instance’s lifecycle.

```auto
    [AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]
    static FMOD.RESULT DialogueEventCallback(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr)
    {
        switch (type)
        {
            case FMOD.Studio.EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND:
            {
                // Play a sound
                break;
            }
            case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROY_PROGRAMMER_SOUND:
            {
                // Clean up the sound you made before
                break;
            }
            FMOD.Studio.EVENT_CALLBACK_TYPE.STOPPED:
            {
                // The event is being stopped
                break;
            }
            case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROYED:
            {
                // The event is being destroyed
                break;
            }
        }
        return FMOD.RESULT.OK;
    }

```
