# Update global distance min/max variable from code?

**URL:** <https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569>\
**Category:** Unity\
**Tags:** unity\
**Created:** [November 23, 2022, 7:11pm UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569 "2022-11-23T19:11:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cj2heads](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cj2heads](https://qa.fmod.com/u/cj2heads)\
**Post date:** [November 23, 2022, 7:11pm UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/1 "2022-11-23T19:11:18Z")

</div>

I’m working on a top down game where you can zoom the camera in and out. I want to explore changing the min distance for all the spatializers when you zoom out so sound effects that occur on the camera all sound about the same and when you zoom in the sound effects outside the camera would become quieter.

Ideally I’d like to call some API to change global min/max distance so that I can achieve this idea. I’ve been digging around but haven’t found an answer. How would I go about trying to make this happen?

---

<div class="post-metadata">

**Author:** ![cj2heads](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cj2heads](https://qa.fmod.com/u/cj2heads)\
**Post date:** [November 23, 2022, 9:37pm UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/2 "2022-11-23T21:37:19Z")

</div>

I have found this in the API docs which seems to get me pretty close:

[https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#fmod\_studio\_event\_property\_maximum\_distance](https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#fmod_studio_event_property_maximum_distance)

And it looks like I can loop through all loaded banks and all events to get the instances that are playing:

> [@Retrieving info for playing events](https://qa.fmod.com/t/retrieving-info-for-playing-events/11844):
>
> With the Designer API we were able to query which events were currently playing at run-time (EventSystem::getInfo). This allowed us to visualize currently playing audio events within our 3d game world. I was not able to find a way to do this with the Studio API. Is it possible?

I’m going to set that up for now but still holding out that there’s a simpler solution. Would love to hear a response if there is 🙂

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [November 24, 2022, 5:16am UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/3 "2022-11-24T05:16:16Z")

</div>

Hi,

That is a good way to get all events that are currently playing. Then when you are ready to change the distances you could use something like this:

```auto
currentInstance.setProperty(FMOD.Studio.EVENT_PROPERTY.MINIMUM_DISTANCE, NewValue);

```

Use the same function but change `MINMUM_DITANCE` to `MAXIMUM_DISTANCE`.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![cj2heads](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cj2heads](https://qa.fmod.com/u/cj2heads)\
**Post date:** [November 24, 2022, 6:44am UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/4 "2022-11-24T06:44:37Z")

</div>

Is this the best way to approach this (change the min/max distance for every event)? I wanted to make sure there wasn’t a one place spot I could be doing this or if it is just every active event.

---

<div class="post-metadata">

**Author:** ![cj2heads](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cj2heads](https://qa.fmod.com/u/cj2heads)\
**Post date:** [November 24, 2022, 9:29pm UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/5 "2022-11-24T21:29:38Z")

</div>

I only need to set the min/max distance every once in awhile (when the camera zoom level changes) but this tactic appears to have the downside that new sound effects will play with the default min/max (which makes sense as I iterate through all the existing event instances to set the values).

I can every single frame go through and set the min max but this seems like a slight shame. So I guess I’m still trying to discover if there’s a cleaner/nicer way to accomplish what I’m trying to accomplish.

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [November 24, 2022, 9:37pm UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/6 "2022-11-24T21:37:24Z")

</div>

Instead of only changing active events you could just change every event. That way when a new event starts its values will already be set. Or when an event starts you could check what the global values are and set it accordingly before it makes noise. There are a few options.

---

<div class="post-metadata">

**Author:** ![cj2heads](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cj2heads](https://qa.fmod.com/u/cj2heads)\
**Post date:** [November 24, 2022, 9:53pm UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/7 "2022-11-24T21:53:19Z")

</div>

How do I access every event? I’ve been iterating through all event descriptions from a particular bank and then iterating through all event instances to set the min/max distance. This appears to only impact active events (I think) or at the very least new events don’t have the min/max distance set.

Here’s what I’m doing:

```auto
int descCount = _cachedBankDescriptions.Length;

for (int i = 0; i < descCount; i++)
{
    EventDescription desc = _cachedBankDescriptions[i];

    _eventInstances.Clear();
    CheckResult(desc.getInstanceList(_eventInstances)); // Just a custom version that does not generate garbage

    int instanceCount = _eventInstances.Count;

    for (int j = 0; j < instanceCount; j++)
    {
        EventInstance ev = _eventInstances[j];
        CheckResult(ev.setProperty(EVENT_PROPERTY.MINIMUM_DISTANCE, _currentMin));
        CheckResult(ev.setProperty(EVENT_PROPERTY.MAXIMUM_DISTANCE, _currentMax));
    }
}

```

_EDIT_ Ok I see the call back on description and can leverage that. But I don’t see how to change every event instead of just active events.

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [November 28, 2022, 2:46am UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/8 "2022-11-28T02:46:07Z")

</div>

Apologies, you can only access active events. My mistake.

Your current method is the best I can think of. When the camera changes position loop through all the events, then when an event is started assign it to the global values.

Sorry for the confusion.

---

<div class="post-metadata">

**Author:** ![cj2heads](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cj2heads](https://qa.fmod.com/u/cj2heads)\
**Post date:** [November 28, 2022, 5:59pm UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/9 "2022-11-28T17:59:24Z")

</div>

Thanks for all the help. Here’s fully what I ended up doing (for the solution)

When the min/max needs to change I will loop over all active events from the event description and set their min/max using setProperty.

I also attach to the event description’s callback during initialization and listen for the event for new events being created. I will then set their min/max to the current values.

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [November 29, 2022, 12:44am UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/10 "2022-11-29T00:44:34Z")

</div>

Thank you for sharing the solution!

---

<div class="post-metadata">

**Author:** ![Damion1](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/damion1/32/2716_2.png) [@Damion1](https://qa.fmod.com/u/Damion1)\
**Post date:** [December 7, 2022, 9:44am UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/11 "2022-12-07T09:44:11Z")

</div>

Hey folks. Surely you can just use a global parameter and automate the min/max distance values on every event’s master macro controls (within fmod)??? No code required… (except for driving the Global Param with the zoom distance)

You can even do this on a preset spatialiser effect with distance override turned on, if you don’t want to worry about having to mess with lots of batch editing later on…

---

<div class="post-metadata">

**Author:** ![cj2heads](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cj2heads](https://qa.fmod.com/u/cj2heads)\
**Post date:** [December 7, 2022, 6:12pm UTC](https://qa.fmod.com/t/update-global-distance-min-max-variable-from-code/19569/12 "2022-12-07T18:12:51Z")

</div>

@Damion1 That’s a nice solution (and what I was poking for to see if there was a better way of doing it).

Thanks!
