Fixed (FIXED in 1.21.2) Crash when plugin slider max set to 0 and default button clicked

everlaster

Well-known member
Featured Contributor
Messages
652
Reactions
3,322
Points
93
Website
patreon.com
Twitter
everlasterVR
Patreon
everlaster
Steps to reproduce in plugin code

C#:
// create storable and slider with max and default e.g. 10
var jsf = new JSONStorableFloat("test", 10, 0, 10);
CreateSlider(jsf);

// update max value to 0
jsf.max = 0;

If the slider's default button is clicked now, VAM crashes. If the max value is set to any other value between 0 and 10, no crash - it will correctly set a new max value at the default value and apply the default value.

I tested with the min value as well:

C#:
// create storable and slider with min and default e.g. -10
var jsf = new JSONStorableFloat("test", -10, -10, 0);
CreateSlider(jsf);

// update min value to 0
jsf.min= 0;

However clicking the default button in this case does not cause a crash.
 
I bet there's some div by 0 somewhere in the depths of Unity. Setting max to some float really close to 0 is probably a good workaround. This could probably be fixed in JSONStorableFloat's InternalSetVal method.
 
This one is on me. When you set a value on JSONStorableFloat it tries to adjust the slider range to fit the value using this code:

C#:
            if (slider != null) {
                while (value > slider.maxValue) {
                    slider.maxValue *= 10f;
                }
                if (value < slider.minValue) {
                    slider.minValue = value;
                }
                slider.value = value;
            }

As you can see if maxValue is 0 it will get forever stuck in the while loop since it is multiplying 0 by 10 which is always 0. It is fortunately an easy fix.
 
Implemented this fix in upcoming 1.21.2 release which fixes both 0 value and negative value max value cases:

C#:
                if (value > slider.maxValue) {
                    if (slider.maxValue > 0f) {
                        while (value > slider.maxValue) {
                            slider.maxValue *= 10f;
                        }
                    } else {
                        slider.maxValue = value;
                    }
                }
                if (value < slider.minValue) {
                    slider.minValue = value;
                }
                slider.value = value;
 
Back
Top Bottom