# 11. Scripting examples | Programmer Sounds - suggestion to improve Scripting example

**URL:** <https://qa.fmod.com/t/11-scripting-examples-programmer-sounds-suggestion-to-improve-scripting-example/19190>\
**Category:** Unity\
**Created:** [August 26, 2022, 3:19pm UTC](https://qa.fmod.com/t/11-scripting-examples-programmer-sounds-suggestion-to-improve-scripting-example/19190 "2022-08-26T15:19:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![atlem](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@atlem](https://qa.fmod.com/u/atlem)\
**Post date:** [August 26, 2022, 3:19pm UTC](https://qa.fmod.com/t/11-scripting-examples-programmer-sounds-suggestion-to-improve-scripting-example/19190/1 "2022-08-26T15:19:17Z")

</div>

In your scripting example of [Programmer Sounds](https://fmod.com/docs/2.02/unity/examples-programmer-sounds.html), would you consider adding an example on how to pass the sound key when the sound is stopped?

Since you cannot Invoke a event, ie: `public static event Action<string> OnSoundStopped;` from inside the AOT MonoPInvokeCallback. How would you suggest we pass this info to any other scripts that need it?

```auto
case EVENT_CALLBACK_TYPE.SOUND_STOPPED:
{
    OnSoundStopped?.Invoke(key); // Crashes unity
    break;
}

```

---

<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:** [August 29, 2022, 4:26am UTC](https://qa.fmod.com/t/11-scripting-examples-programmer-sounds-suggestion-to-improve-scripting-example/19190/2 "2022-08-29T04:26:18Z")

</div>

I have not been able to reproduce any issues accessing user data from an event instance during an `EVENT_CALLBACK_TYPE.SOUND_STOPPED` callback, and I am finding `key` has a valid value for me in the Programmer Sounds example.

> [@atlem](#):
>
> `OnSoundStopped?.Invoke(key); // Crashes unity`

I do not know what `OnSoundStopped` is, but it is not an FMOD API method so I have no way of knowing why it is crashing. Is this a function you have created? If so, can you please share a code snippet of it?

---

<div class="post-metadata">

**Author:** ![atlem](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@atlem](https://qa.fmod.com/u/atlem)\
**Post date:** [September 1, 2022, 10:23am UTC](https://qa.fmod.com/t/11-scripting-examples-programmer-sounds-suggestion-to-improve-scripting-example/19190/3 "2022-09-01T10:23:58Z")

</div>

Thanks for the reply, sorry if I was not clear. I was just wondering if you had any recommendations on how to pass that key from the EVENT\_CALLBACK. It would be useful to be able to subscribe to a OnSoundStopped event in order to be notified when the programmer sound is stopped.

The Unity crash does not have anything to do with FMOD, it should be related to the unmanaged native callback into the managed static function.

For now I made it work by storing the key in a private variable, and then passing it when the playback state changes. I do this by calling `eventInstance.getPlaybackState(out _playbackState)` every frame in Update() and using that to Invoke my `event Action OnSoundStopped`.

---

<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:** [September 6, 2022, 2:42am UTC](https://qa.fmod.com/t/11-scripting-examples-programmer-sounds-suggestion-to-improve-scripting-example/19190/4 "2022-09-06T02:42:05Z")

</div>

I see what you mean now. There are various options to defer a call until outside of the callback; I agree storing the string value and querying `EventInstance.getPlaybackState` is a good option in your case. If you wanted a little more versatility and to be able to respond do different callback types then maybe pushing to a command queue and executing in `Update` would work well.  
I have passed your suggestion onto the Dev team to see if we can improve our Programmer Sounds example to support `Action`s- thanks for the suggestion!
