Unnecessary Square Root

Hello,

Doing some performance improvements for our game (Unity).
I’ve noticed that in RuntimeManager.cs within UpdateActiveEmitter you are doing a potential unnecessary square root.

Is there a reason for this?

I’ve made a potential fix, could you look into this.

Previous code

public static void UpdateActiveEmitter(StudioEventEmitter emitter, bool force = false)
        {
            // If at least once listener is within the max distance, ensure an event instance is playing
            bool playInstance = false;
            for (int i = 0; i < Listeners.Count; i++)
            {
                if (Vector3.Distance(emitter.transform.position, Listeners[i].transform.position) <= emitter.MaxDistance)
                {
                    playInstance = true;
                    break;
                }
            }
            
            if (force || playInstance != emitter.IsPlaying())
            {
                if (playInstance)
                {
                    emitter.PlayInstance();
                }
                else
                {
                    emitter.StopInstance();
                }
            }
        }

Amended code

public static void UpdateActiveEmitter(StudioEventEmitter emitter, bool force = false)
        {
            // If at least once listener is within the max distance, ensure an event instance is playing
            bool playInstance = false;
            for (int i = 0; i < Listeners.Count; i++)
            {
                // Maybe cache this?
                float maxDistanceSqr = emitter.MaxDistance;
                maxDistanceSqr *= maxDistanceSqr;

                if ((emitter.transform.position -Listeners[i].transform.position).sqrMagnitude <= maxDistanceSqr)
                {
                    playInstance = true;
                    break;
                }
            }
            
            if (force || playInstance != emitter.IsPlaying())
            {
                if (playInstance)
                {
                    emitter.PlayInstance();
                }
                else
                {
                    emitter.StopInstance();
                }
            }
        }

Thanks,
Tom

Hi,

Thank you for the suggestion!

What version of the integration are you using?

2.01.09

Thank you,

I have passed it onto our development team to look into further, we appreciate the suggestion!

1 Like