Assigning Event Paths at Runtime?

I’m going crazy because I have to be misunderstanding something, and I have a hard time believing that FMOD lost a feature so critical.

How can I create/assign an EventReference at runtime?

Something like:

string eventPathLoadedFromDataFile = loadedData.jetAudioPath;
jetEmitter.EventReference = new EventReference(eventPathLoadedFromDataFile);

The above code obviously doesn’t work, but that’s the gist of what I’m trying to do. There doesn’t seem to be any way to achieve that kind of behavior with the new way EventReference works compared to EventRef of older FMOD. This is a completely breaking change for my game, because my game creates all its vehicles and aircraft at runtime using JSON defined paths. For example the vehicle definition has this property:

"EngineSound": "event:/Vehicles/TankEngine",

Is this impossible now?

Hi,

Sorry, I don’t understand the issue.

Could I grab some more info:

  • What is being created at runtime?
  • What version of FMOD were you able to do this in previously?
  • Do you have a working code snippet I could see?
  • Is there any docs you can point to with this functionality?

Any more info would be greatly appriciated.

What is being created at runtime?

StudioEventEmitters are part of various Unity prefabs, however they do not have an event associated with them until runtime, where data loaded from the disk then populates them with an event reference string.

What version of FMOD were you able to do this in previously?

2.00.08

Do you have a working code snippet I could see?

There is no working code snippet anymore, but this is an example of code that used to work.

if (Data.EngineSound.Length > 0f)
{
    EngineSoundEmitter.Event = Data.EngineSound;
    EngineSoundEmitter.Play();
}

The Data.EngineSound is just a string that is the name of an event. E.g. event:/Vehicles/TankEngine. The value of this string is read in from various files for various different vehicles at runtime based on JSON data files.

.Event is no longer usable, and I can’t find a replacement that lets me do the same thing. It’s suggested to use EventReference but there doesn’t seem to be a way to create these at runtime. FMODUnity.EventReference.Find() should work, but this code does not work in a build since it is surrounded by #if guards. I’ve been trying to work around this by moving code out of the #if UNITY_EDITOR blocks, but it was at this point where I was starting to get enough into the weeds with changing FMOD code that I had to stop and ask if I was missing something obvious.

Is there any docs you can point to with this functionality?

https://www.fmod.com/docs/2.00/unity/api-studioeventemitter.html#studioeventemitter_event

1 Like

Thank you for the additional information.

Would Unity Integration | Scripting API Reference - RuntimeManager::PathToEventReference() work? It can be used like so:

emitter = GetComponent<FMODUnity.StudioEventEmitter>();
emitter.EventReference = FMODUnity.RuntimeManager.PathToEventReference("event:/Music/Level 01");
emitter.Play();

This would still allow you to use the paths to create and play events using an emitter.

Hope this helps

The good news is that solves the problem of assigning events at runtime. Thank you.

However that brings me back to my original problem before this one. I have searched for it in all results I’ve come up with suggest preloading the banks, but that hasn’t worked neither when I do it through settings or manually through code.

For future search indexing, the specific log message is:
Loading delay exceeded. Instrument {7eb1e17c-fab3-440d-8bcf-25bc5ab701fd} started 76.87ms later than expected, sound may play at incorrect time. SCHEDULE_DELAY: 170.67ms.

The log gets bombarded with these messages, and then the sound playing gets completely out of sync with the action. This did not happen until I moved Unity version to 2023.2 from 2020.3.

This problem has been stumping me for the last week, and I’m very close to just reverting the engine to 2020 and sucking it up, because FMOD is the sole reason I can’t go through with the engine update and I can’t even figure out where to begin debugging this.

At first I thought it was a mismatched FMOD/Unity combination, but even on FMOD 2.00.08 (the FMOD the game originally used on Unity 2020.3), this delay issue was happening. So I tried updating FMOD to 2.03.07 thinking that might fix it and that update process was painful but seems to be done. However it has done nothing to fix this issues with out of sync audio, so there’s something that changed in Unity 2023 that is completely destroying the audio in my game.

The only interesting information I can report is that when there is a single aircraft flying around, the audio is (usually) okay, but something about multiple causes the audio engine to become completely non-responsive.

I am not using Programmer Instruments.

If this was just a one-time thing per game session I’d be more forgiving, but this issues pops up regularly during gameplay with seemingly every new sound causing the audio to become completely unsynched.

Good to hear we solved that issue.

Thank you for all the information.

To help the delayed sound problem, could you please enable the Load Bank Sample Data option in the integration settings:


This reduces latency at the cost of using more memory: Unity Integration | Settings - Load Bank Sample Data

Hope this helps

Yes that was already checked.

However, after banging my head on this problem for almost 2 weeks, I noticed that for some reason a bunch of audio assets (notably the problematic ones) in my FMOD Studio project got marked as STREAM when they were never meant to. I’m not sure when that happened or how, but setting them back to not streaming anymore (the STREAM button goes gray, plus they lose the STREAM tag) seems to have resolved the issue?

1 Like

Thank you for sharing the solution.

When I try to use EventReference.Find() in a build, it throws an error.

EventReference.Find called before EventManager was initialized

How can I initilize the EventManager manually since that seems to not be happening in a build?

Looking at the source, this seems to be a feature, not a bug, since the EventManager is only available in the Editor.

Correct. What functionality are you looking for in build?

To be able to assign event reference at runtime based on the string path to an event as I described in the OP and my second post.

I see, was this solved by Assigning Event Paths at Runtime? - #4 by Connor_FMOD?

You’re, right. I’m sorry, I got confused with the function names and mixed up the EventReference.Find() with RuntimeManager.PathToEventReference().

The good news is that it loads up and sounds mostly kind of work now. However I still get unplayable stuttering when certain sounds are triggered, and sometimes sounds don’t play at all.

It works perfectly in the Editor, with no errors or warnings thrown. However in the build e.g. when this bullet flyby sound effect is meant to played the game’s framerate crawls to a standstill. This is only the easiest and most reproducible Build only bug, but I assume it’s indicative of what’s happening elsewhere.

Attached is the log, which is full of FMOD related warnings and errors.
Player.log (268.5 KB)

What is going on here, and why is this only happening in the build? It works fine in the Editor (albeit audio levels are strange compared to what they should be, but that at least seems fixable and is probably an artifact of moving from 2.00 to 2.03). It’s only in the Build where it becomes unplayable with missing seemingly missing SFX and harsh stutters.

I’ve been able to roll back to 2.00.08, but on Unity 2023. With the streaming audio fix mentioned earlier in the thread, everything seems to be working again.

I’m staying away from 2.03 and will continue using 2.00 for the remainder of this project and praying nothing else goes wrong.

Thank you for the information and the video.

I understand, if you decide to return to 2.03 there are some more options we have for investigating the issue.