When do I need to create instances

Hello
I need to create an instance at the very beginning of the game, in awake or start methods. But I’m having problems because after executing the CreateInstance method, the instance is still is not valid. But at the same time, if I call this code not at the beginning of the game, but manually after some time, then it is created perfectly. As far as I understand, I need to wait for some process related to fmod, but I don’t know which one.
Thanks.

        [SerializeField] private StudioEventEmitter _eventEmitter;
        private EventInstance EventInstance => _eventEmitter.EventInstance;

        public void CreateInstance()
        {
            if (EventInstance.isValid() == false) // this condition is true
            {
                 _eventEmitter.CreateInstance();

                UnityEngine.Debug.Log($"IS VALID NOW: {EventInstance.isValid()}"); // it still false
            }
        }

       public void Awake()
      {
          CreateInstance();
      }

Hi,

The FMODUnity.StudioEventEmitter does not have the CreaateInstance() function, so I am unsure what it is doing.

The Studio Event Emitter class should be creating a valid Event Instance on its own given a valid event path.

You can choose when the event emitter begins playing its event using the given Trigger Conditions, one of the options is Object Start, this may provide the functionality you are looking for.

An alternative option is to use an EventReference which you can use to create an event instance with FMODUnity.RuntimeManager.CreateInstance() (Unity Integration | Scripting API Reference - RuntimeManager) which would allow you to have complete control over the event instance.

Hope this helps!

Hello,

I can’t use Object Start because I have to wait for some of my processing before creating the instance. And I don’t need to just launch an event, before that I have to install a callback for the programmer, here is my event launch script:

Full method of starting event

Here are all the screenshots of using the StudioEventEmitter script and the CreateInstance method. I’m sorry, this method was added by my colleague from another fmod script, I didn’t know about it, but here’s the code

Screenshot of method CreateInstance

Most likely I can’t use FMODUnity.RuntimeManager.CreateInstance() because in conjunction with StudioEventEmitter I use SteamAudioSouce, which requires an emitter

Screenshot of SteamAudioSource with StudioEventEmitter in editor

image

I fixed it just adding Action when description is initialized in start method

StudioEventEmitter script
//fields
        public event Action DescriptionInitialized;
        private bool _isDescriptionInitialized = false;

        protected override void Start()
        {
            RuntimeUtils.EnforceLibraryOrder();
            if (Preload)
            {
                Lookup();
                eventDescription.loadSampleData();
            }

            HandleGameEvent(EmitterGameEvent.ObjectStart);

            // If a Rigidbody is added, turn off "allowNonRigidbodyDoppler" option
#if UNITY_PHYSICS_EXIST
            if (AllowNonRigidbodyDoppler && GetComponent<Rigidbody>())
            {
                AllowNonRigidbodyDoppler = false;
            }
#endif

            _isDescriptionInitialized = true;
            DescriptionInitialized?.Invoke();
        }

        public void MakeOnDescriptionIsInitialized(Action toMake)
        {
            if (_isDescriptionInitialized)
            {
                toMake?.Invoke();
            }
            else
            {
                toMake += () => DescriptionInitialized -= toMake;
                DescriptionInitialized += toMake;
            }
        }
1 Like

Hi,

Thank you for sharing the solution.