godot icon indicating copy to clipboard operation
godot copied to clipboard

Range loads incorrect value under certain circunstances

Open 98teg opened this issue 1 year ago • 3 comments

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

RangeBug.zip

98teg avatar Jan 28 '23 13:01 98teg

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

akien-mga avatar Jan 28 '23 14:01 akien-mga

Sorry, I can't help right now, my laptop just broke.

phnix-dev avatar Jan 28 '23 16:01 phnix-dev

  1. The current value may be modified when setting the property max/min/step. The step value is not set first when loading, which causes the original step default value to still be valid when max/min is set.
  2. 0 is the default value of the property value, it will not be reset to 0 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.

Rindbee avatar Jan 29 '23 11:01 Rindbee

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

phnix-dev avatar Jan 31 '23 11:01 phnix-dev

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.

Rindbee avatar Jan 31 '23 12:01 Rindbee