Managing Multiple Instances of Same Event

Hi! This is a two-part question.
I have an event that I call multiple times, and each time I’d make a unique instance and add it to a list.

  1. If I’d like to stop and release one specific instance (that hasn’t played till the end), how can I filter it out? From what I understand, all instances will share the same ID and Event Description. Is there something in FMOD or do I need to implement my own way of discriminating each instance?

  2. When cleaning up the list from the instances that got released. Is it enough to use instance.isValid() and remove it when the return is false?

FMOD offers some options related to limiting event instance counts, but beyond that, if you want to stop/release an instance for reasons specific to your game, then you will need to implement a way of discriminating between them in your own code.

Yes, it is enough to use instance.isValid(). Another option, assuming that the event has a natural end and you no longer need access to it, would be to release the event and then immediately remove it from your list, since the system will handle cleaning it up automatically when it stops playing.

1 Like

Thanks for the quick reply!

then you will need to implement a way of discriminating between them in your own code.

This confirms it then, thank you! I’ve noticed there’s also the option to add some additional data to an instance using instance.setUserData() which I thought about using to tell instances apart. Although I’m unsure if it’d be performant when used a lot to filter a list of many instances.

Another option, assuming that the event has a natural end and you no longer need access to it, would be to release the event and then immediately remove it from your list

I have some events that are entirely a loop region and from some testing I saw that FMOD won’t actually release these events unless I manually stop them (even if I call release right after start, which makes sense). So I guess these are the ones that go against the “natural end” you’ve mentioned - also why I need to access the specific events based on what game object requested playing those loops.

You could use user data in the way you’ve described, yes. However, because user data in C# is interacting with both managed C# (Unity) and native CPP (FMOD), you would need to add extra IntPtr and GCHandle boiler plate to handle this - you’d essentially be introducing a small aspect of memory management to your code where there’d otherwise not be any. As a result, it’s probably be more convenient to handle distinguishing between instances in your own C# code.

Aha! Good tip. Thanks Louis!

1 Like