# Know when an EventInstance has been released

**URL:** <https://qa.fmod.com/t/know-when-an-eventinstance-has-been-released/19760>\
**Category:** Unity\
**Tags:** unity\
**Created:** [January 19, 2023, 12:42am UTC](https://qa.fmod.com/t/know-when-an-eventinstance-has-been-released/19760 "2023-01-19T00:42:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnav\_03](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@arnav\_03](https://qa.fmod.com/u/arnav_03)\
**Post date:** [January 19, 2023, 12:42am UTC](https://qa.fmod.com/t/know-when-an-eventinstance-has-been-released/19760/1 "2023-01-19T00:42:10Z")

</div>

Hi, I would like to know a way that I can ether receive a callback or know when an event has been released. I have marked the event for release when I play the event.

```auto
 public EventInstance Play(string clipName)
    {
        var instance = RuntimeManager.CreateInstance(GetEventID(clipName));
        instance.start();
        instance.release();
        if (EventInstances.ContainsKey(clipName) == true)
        {
            EventInstances[clipName] = instance;
        }
        else
        {
            EventInstances.Add(clipName, instance);
        }
        return instance;
    }

```

I want to know when it is being released so that i can remove it from my Dictionary EventInstances.  
Or if i can get a callback when the audio is finished playing that will also be fine.

Cheers.

---

<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:** [January 20, 2023, 4:55am UTC](https://qa.fmod.com/t/know-when-an-eventinstance-has-been-released/19760/2 "2023-01-20T04:55:57Z")

</div>

Hi,

You can indeed receive a callback when an event has finished playing after having been released. The callback type [`FMOD_STUDIO_EVENT_CALLBACK_TYPE.DESTROYED`](https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#fmod_studio_event_callback_type) will trigger a callback specified by [`Studio.EventInstance.setCallback()`](https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_setcallback) when an instance is just about be destroyed. Passing your dictionary to the event instance as user data with [`Studio.EventInstance.setUserData()`](https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_setuserdata), and retrieving it with [`Studio.EventInstance.getUserData()`](https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_getuserdata), will allow you to remove the instance from the dictionary within the callback.

---

<div class="post-metadata">

**Author:** ![arnav\_03](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@arnav\_03](https://qa.fmod.com/u/arnav_03)\
**Post date:** [January 20, 2023, 7:29pm UTC](https://qa.fmod.com/t/know-when-an-eventinstance-has-been-released/19760/3 "2023-01-20T19:29:44Z")

</div>

Thanks @Leah_FMOD,  
I was able to get this to work with EVENT\_CALLBACK. But I just send my key for the Dictionary instead of the whole Dictionary.  
For Future Refence here is my Solution

```auto
 public EventInstance Play(string clipName)
    {
        var instance = RuntimeManager.CreateInstance(GetEventPath(clipName));
        instance.setUserData(GCHandle.ToIntPtr(GCHandle.Alloc(clipName)));
        instance.setCallback(eventReleasedCallback, EVENT_CALLBACK_TYPE.DESTROYED);
        instance.start();
        instance.release();
        EventInstances.Add(clipName, instance);
        return instance;
    }

```

```auto
 [AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]
    private RESULT OnInstanceDestroyed(EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameters)
    {
        var instance = new FMOD.Studio.EventInstance(instancePtr);
        instance.getUserData(out var userData);
        var clipName = GCHandle.FromIntPtr(userData).Target as string;
        EventInstances.Remove(clipName);
        return RESULT.OK;
    }

```
