Using EventReference.Find() firectly in C# script

FMOD version 2.02, Unity version 2020.3.24f1

I am trying to assign EventReference inside the C# script with a string path, and without having to manually drag things inside Unity inspector, because things might be re-structured and renamed quite a few times.

I tried something like:
EventReference obj_interact = "event:/interact";
and this would tell me that the latter is not a EventReference but only a string.

Using EventReference obj_interact = EventReference.Find("event:/interact"); in Start()
would result in an error “Called before eventmanager was initialzed error”.

If I use EventReference.Find() in Reset() instead, it doesn’t really seem to work?

Is there a standard solution to setting this reference directly in C#, or maybe a functioning example somewhere I can check out?

Sorry if this is a duplicate question but I haven’t found a solution.

Hi,

I would suggest changing the Event Linkage settings to GUID, so that if the event changes e.g. directory or name, the Reference won’t be broken. To find this setting it is under Unity Tool Bar -> FMOD -> Edit Settings -> Bank Import -> Event Linkage.

To initialize an Event Reference from code, use something like this.

"Creating and playing an Event reference in C#
// Initializing an empty EventReference
FMODUnity.EventReference eventref = new EventReference();
// Assinging both its path and GUID
eventref.Path = "event:/Anti";
System.Guid eventGuid = new System.Guid("{13e590d5-53ec-4044-91b8-13bec9ec1684}");
eventref.Guid = new FMOD.GUID(eventGuid); 
// Creating the EventInstance with the filled EventReference 
FMOD.Studio.EventInstance inst = FMODUnity.RuntimeManager.CreateInstance(eventref);
// Check if the Event was created successfully 
if (inst.isValid() == false)
	UnityEngine.Debug.Log("Failed to create Event Instance"); 
else
{
	// Starting and releasing the successfully created Instance 
	inst.start();
	inst.release();
}

With Event Linkage set to GUID it isn’t necessary to assign the EventReference.Path but it may help to identify the Event you are referencing in the future.

Hope this helps!

Thank you Connor! That was really helpful!

Now I’m further wondering:

  • If the Event Linkage is still set as Path, do I still need to assign both Path and GUID in the C# code as shown in your reply, for the implementation to work properly?
  • I’ve been using FMODUnity.RuntimeManager.PlayOneShot() for playing the eventreferences. How is that different from using an instance then start () and release()? What are the pros and cons for each?
  • I’m trying to write a few universal static call functions for all sfx inside a AudioManager C# script. If all these code are to be placed inside the function, I think it might be to too consuming to generate an event reference each call. Meanwhile, I can’t think of a way to create a static reference outside this call. Is there a suggested solution to this?

No worries!

Yes, both fields will have to be filled if linkage is set to Path.

PlayOneShot(), Unity Integration | Scripting API, allows you to play an Event and not have to worry about memory management as the FMOD will take care of that. On the other hand with an Instance, Unity Integration | Scripting API, requires you to play(), stop(), and release() it. However, you can manipulate the parameters on an instance while you cannot for a one-shot. Depending on the situation will decide which you will want to use.

Could I see a code example of what you are referring to? I am a bit confused.

I see!

In this case, PlayOneShot() is preferred as long as I don’t need to worry about instance parameters.

For the last question, I’m writing something like:

    public static string ex01 = "event:/ex01";
    public static string ex02 = "event:/ex02";

    public static void sfx_play_2d(string eventref_path)
    {
        EventReference cur_event = new EventReference();
        cur_event.Path = eventref_path;
        RuntimeManager.PlayOneShot(cur_event);
    }

inside an audio manager script. The function has to be static in order to be called somewhere else. That’s also why some relevant path strings are static, since (to my extent of knowledge) a EventReference should not be at declaration. But I’m afraid that it’s too much if an EventReference would be generated each time a sfx plays.

Anything suggestion on this?

Would this “static” problem be handled correctly if I use the Unity in-inspector API emitter (I might be calling it wrong and it’s a whole lot to learn from beginning)?

Or should I talk to the engineering team to see if they could make things non-static?

PlayOneShot() will also accept the path of the event e.g.

public static void sfx_play_2d(string eventref_path)
{
    RuntimeManager.PlayOneShot(eventref_path);
}

This way you don’t have to create an EventReference each time you call the function.