godot
godot copied to clipboard
Range loads incorrect value under certain circunstances
Godot version
4.0.beta16
System information
Ubuntu
Issue description
When using an HSlider, I provided the following values:
- Min value: -0.5
- Max value: 0.5
- Step: 0.01
- Value: 0
However, when closing and opening the scene again, or when playing, the value sets itself to 0.5.
Steps to reproduce
Instantiate an HSlider and follow the steps described above, you should not be able to set the value to 0.
Minimal reproduction project
I can reproduce the issue on 4.0 beta 16 and latest master
.
It's a regression, the bug was introduced between beta 7 and beta 8. There were two relevant PRs in that timeframe:
- #65101 @MrPhnix
- #67660 @Rindbee
Sorry, I can't help right now, my laptop just broke.
- The current
value
may be modified when setting the propertymax
/min
/step
. Thestep
value is not set first when loading, which causes the originalstep
default value to still be valid whenmax
/min
is set. -
0
is the default value of the propertyvalue
, it will not be reset to0
when loading tscn file.
All range derived classes have this similar issue:
min = - default_step / 2, max = default_step / 2, step = 0.1 * default_step, value = 0
#65101 exposes the issue.
So when saving or loading, whether it is the default value or not, we need to save or set the value
.
I checked, the commit I made works:
Math::round((p_val - shared->min) / shared->step) * shared->step + shared->min; ((0 − −0.5) / 0.01) × 0.01 + −0.5 = 0
Maybe it was not a good idea to remove the Range::_validate_values function. 🤔 #67660
Both PRs are fine. This issue occurs even after reverting #67660.
This is a matter of the order in which properties are set when instantiating a scene. If you need to set max
/min
/step
/value
in batches, step
should be set first, or value
should be set last. Otherwise, the desired result may not be obtained.