Should I create an Event Instance in code or just use the event emitter component?

Hi, hope you guys are doing alright. So, I have some sword sounds with the instance being created directly on code with the FMODUnity.RuntimeManager.CreateInstance() should I instead use the Event Emitter component? Which method is more recommended and what’s the difference between them? Also, what exactly is an instance?

And to anyone who read this, wish you the best.

An instance is a programmation term meaning basically a materialized copy of an abstract object. The object is the FMOD event, the instance is the copy created at realtime to be played. You can create several instances of a unique event, with different properties (for instance, different 3D attributes).

For the first question, I’ll let Unity users answer.

1 Like

Thank you for the clear explanation.

1 Like

Wether you’ll use emitter component or code instance depends on how much you want the code to impact your sound. For example if you want to start a music event when you enter a room, you simply have to create a trigger in unity, then use the emitter component by setting the start onTriggerEnter. But let’s say you want the music to stop when there is a code condition that is met (for example when your health is below a certain threshold), it will become more efficient to use the scripted method because you’ll want your instance to react to the game’s code. In your case, I guess there is a function in your code that triggers a sword hit. I don’t believe you can access this function with the event emmiter component, so you’d have to do something like this in :


public class NameOfYourCode : MonoBehaviour
{
   //To create a reference to your FMOD Event
   private EventReference swordSfx = "the path of your event in fmod";
   //To set up a reference you'll use later in code to create your instance
   FMOD.Studio.EventInstance swordSfxInstance;

   //The function responsible for firing a sword hit
   void SwordHitFunction()
   {
     //Creating an instance from the reference of your event
     swordSfxInstance = FMODUnity.RuntimeManager.CreateInstance(swordSfx);
     //Starting the instance
     swordSfxInstance.start();
     //Allowing unity to purge to destroy the instance when the event stop
     swordSfxInstance.release();
   }
}

You could also use PlayOnShot() here because you don’t really need to do something else with the instance except playing it.

public class NameOfYourCode : MonoBehaviour
{
   //To create a reference to your FMOD Event, Here you don't need to manually create an instance anymore because PlayOneShot() does it for you.
   private EventReference swordSfx = "the path of your event in fmod";

   //The function responsible for firing a sword hit
   void SwordHitFunction()
   {
     //Creates an instance, plays it and releases it in one line of code
     FMODUnity.RuntimeManager.PlayOneShot(swordSfx);
     //And if you're using a 3D spatialized event, and you want the sfx to be localized on the sword
     FMODUnity.RuntimeManager.PlayOneShotAttached(swordSfx, swordGameObject);
   }
}
1 Like

Thank you for your answer, now I understand :slight_smile: