Getting a parameter value instantly

I am trying to get a parameter value as soon as I start an event. I want to use a command instrument to set a parameter to a certain value at the start of an event, and then immediately access this value in the code. The parameters I want to get are both Distance and a custom parameter called “NoiseLevel,” as I am setting up a noise detection system in my stealth game and want to separate the actual dB level from the detection radius. In order for my Noise Detection System to function the way I want I want to access these parameters immediately. I am aware I can create a NoiseAndSound object and do separate calculations, but I would like to only do one distance calculation when calculating the distance of the noise from the player/enemies.

My problem is if I try to access these values immediately, even after setting the 3DAttributes, I get a value of 0 for both a Distance parameter and the NoiseLevel parameter. If I start a Coroutine and wait 0 seconds, I randomly get 0 or the correct values. If I wait one frame I reliably get both values. Here is the test class I am using:

using FMOD.Studio;
using FMODUnity;
using System.Collections;
using UnityEngine;

public class AudioTester : MonoBehaviour
{

public EventReference testEvent;
private EventInstance instance;
public GameObject testObject;

private void Start()
{
    instance = RuntimeManager.CreateInstance(testEvent);

    //Doesn't seem to make a difference which method I use or when I do it
    RuntimeManager.AttachInstanceToGameObject(instance, testObject, true);
    //instance.set3DAttributes(RuntimeUtils.To3DAttributes(R));

}

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        instance.start();
        instance.getParameterByName("Distance", out float _, out float distanceValue);
        instance.getParameterByName("NoiseLevel", out float _, out float noiseValue);

        Debug.Log("Before wait: " + distanceValue + " (" + noiseValue + ")"); //always returns 0 for both values

        instance.release();
        StartCoroutine(IGetNoise());
    }

}

private IEnumerator IGetNoise()
{
    //yield return new WaitForSeconds(0); //randomly returns either 0 or correct values
    yield return null; //reliably returns correct values

    instance.getParameterByName("Distance", out float _, out float distanceValue);
    instance.getParameterByName("NoiseLevel", out float _, out float noiseValue);
    Debug.Log("After wait: " + distanceValue + " (" + noiseValue + ")");
}

}

Here is a screenshot of my event in FMOD

Hi,

Thank you for sharing the code and information!

This behavior sounds expected.

Commands issued by Command Instruments are not processed instantly, but only take effect after the next FMOD Studio update tick. That might explain why you’re getting zero values when reading the parameters in the same frame.

Could you please try calling Studio::EventInstance::setParameterByName right after your CreateInstance() to see if it gets the correct value instantly.