• Hi Guest!

    We are extremely excited to announce the release of our first Beta for VaM2, the next generation of Virt-A-Mate which is currently in development.
    To participate in the Beta, a subscription to the Entertainer or Creator Tier is required. Once subscribed, download instructions can be found here.

    Click here for information and guides regarding the VaM2 beta. Join our Discord server for more announcements and community discussion about VaM2.

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

everlaster

Well-known member
Featured Contributor
Joined
Aug 9, 2020
Messages
703
Solutions
15
Reactions
3,478
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