# StudioEventEmitter.SetParameter creates Garbage

**URL:** <https://qa.fmod.com/t/studioeventemitter-setparameter-creates-garbage/13177>\
**Category:** Unity\
**Created:** [May 12, 2017, 6:06pm UTC](https://qa.fmod.com/t/studioeventemitter-setparameter-creates-garbage/13177 "2017-05-12T18:06:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DigitalDNA](https://avatars.discourse-cdn.com/v4/letter/d/ea666f/32.png) [@DigitalDNA](https://qa.fmod.com/u/DigitalDNA)\
**Post date:** [May 12, 2017, 6:06pm UTC](https://qa.fmod.com/t/studioeventemitter-setparameter-creates-garbage/13177/1 "2017-05-12T18:06:39Z")

</div>

Calling this function creates Garbage due to the internal code.

```
    public RESULT setParameterValue(string name, float value)
    {
        return FMOD_Studio_EventInstance_SetParameterValue(rawPtr, Encoding.UTF8.GetBytes(name + Char.MinValue), value);
    }

```

Is there an alternative way to set the parameter, i.e. by caching the parameter instance. The API seems to have hidden the instances pretty well for some reason. Is there another API that I should be using for this?

Is there an example somewhere of parameter being set every frame in Unity?

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [May 15, 2017, 6:41am UTC](https://qa.fmod.com/t/studioeventemitter-setparameter-creates-garbage/13177/2 "2017-05-15T06:41:15Z")

</div>

You are able get and store a reference to a parameter using: [FMOD.Studio.ParameterInstance](https://www.fmod.org/docs/content/generated/FMOD_Studio_ParameterInstance_SetValue.html)

```
FMOD.Studio.EventInstance event;
FMOD.Studio.ParameterInstance param;

event.getParameter("paramName", out param);

param.setValue(floatValue);

```

---

<div class="post-metadata">

**Author:** ![DigitalDNA](https://avatars.discourse-cdn.com/v4/letter/d/ea666f/32.png) [@DigitalDNA](https://qa.fmod.com/u/DigitalDNA)\
**Post date:** [May 16, 2017, 9:39pm UTC](https://qa.fmod.com/t/studioeventemitter-setparameter-creates-garbage/13177/3 "2017-05-16T21:39:54Z")

</div>

> [@](#):
>
> You are able get and store a reference to a parameter using: [FMOD.Studio.ParameterInstance](https://www.fmod.org/docs/content/generated/FMOD_Studio_ParameterInstance_SetValue.html)
> 
> ```
> FMOD.Studio.EventInstance event;
> FMOD.Studio.ParameterInstance param;
> 
> event.getParameter("paramName", out param);
> 
> param.setValue(floatValue);
> 
> ```

So the solution is just not to use the StudioEventEmitter?

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [May 17, 2017, 12:31am UTC](https://qa.fmod.com/t/studioeventemitter-setparameter-creates-garbage/13177/4 "2017-05-17T00:31:01Z")

</div>

If you are worried about excess garbage then it’s probably a better idea to create your own class for playing sounds and getting/setting parameters.  
StudioEventEmitter can still be used as a guide.

---

<div class="post-metadata">

**Author:** ![DigitalDNA](https://avatars.discourse-cdn.com/v4/letter/d/ea666f/32.png) [@DigitalDNA](https://qa.fmod.com/u/DigitalDNA)\
**Post date:** [May 17, 2017, 9:16pm UTC](https://qa.fmod.com/t/studioeventemitter-setparameter-creates-garbage/13177/5 "2017-05-17T21:16:06Z")

</div>

Thank you. Anyone developing games in unity would(should be) concerned about per frame garbage. It is a core issue with developing games in managed languages. There are some easy things you could do to fix this.

---

<div class="post-metadata">

**Author:** ![DigitalDNA](https://avatars.discourse-cdn.com/v4/letter/d/ea666f/32.png) [@DigitalDNA](https://qa.fmod.com/u/DigitalDNA)\
**Post date:** [May 17, 2017, 10:50pm UTC](https://qa.fmod.com/t/studioeventemitter-setparameter-creates-garbage/13177/6 "2017-05-17T22:50:08Z")

</div>

Here is the dirt simplest way. Just install a cache between the parameters

public static class FmodFix  
{  
private static Dictionary\<string, byte[]\> StringTable = new Dictionary\<string, byte[]\>();  
public static byte[] Encode(string name)  
{  
byte[] results;  
if (!StringTable.TryGetValue(name, out results))  
{  
results = Encoding.UTF8.GetBytes(name + Char.MinValue);  
StringTable[name] = results;  
}  
return results;  
}

}

```
    public RESULT setParameterValue(string name, float value)
    {
        return FMOD_Studio_EventInstance_SetParameterValue(rawPtr, FmodFix.Encode(name), value);
    }
```
