Is there a way to set initial parameter values by script? I tried SetParameter but it only works when the event is currently playing. I need to set it when the actor is spawning. I can’t do it in inspector because the value being set is from some file.
Part of my code:
Hi,
An option may be rather than using the Emitter instance, if you create the instance yourself:
private FMOD.Studio.EventInstance inst;
void Start()
{
inst = FMODUnity.RuntimeManager.CreateInstance("event:/Sound/" + d.moveSOund);
if (!inst.isValid())
{
Debug.Log("Inst is not valid yet");
}
else
{
Debug.Log("Inst is valid right after creating it");
FMOD.RESULT result = inst.setParameterByName("Fraction", (int)d.fraction, false);
if (result != FMOD.RESULT.OK)
{
Debug.Log($"Failed to set param with result {result}");
}
else
{
Debug.Log("Successfully set the parameter without playing the event");
}
}
}
Unfortunately, as the emitter does not create the instance till it is about to be played it will not be valid for the parameter to be set.
Hope this helps!
Do I need to free/destroy the EventInstance when the gameobject is destroyed?
Yes, you can call inst.release()
right after it is started so that when the event stops it is cleaned up automatically. More info can be found here: FMOD Engine | Studio API Reference - Studio::EventInstance
haha I’m actually curious on how emitter’s “Initial Parameter Values” works in the inspector.
Ah sorry for the confusion.
In the StudioEventEmitter.cs
script in the PlayInstance()
function, after the instance is created it will loop through a list of Params
like we were doing when creating the instance our self:
foreach (var param in Params) // Line 297
{
instance.setParameterByID(param.ID, param.Value);
}
Hope this helps!