bug_with_slider

Hi everybody.
I made my own game and found a bug that I can’t fix and I’ll be grateful if someone helps me with it.
Problem: I have a slider and if I move it everything is ok, but if I go to the main menu and back the slider is where it was before but not where it needs to be
here is the code
extends HSlider

func _on_value_changed(new_value: float) → void:
GlobalWorldEnvironment.environment.adjustment_brightness = new_value

func _ready() → void:
pass

I do not have your project and cannot draw a definitive conclusion but it looks to me that you are saving your state (slider value) too low in the hierarchy of your game.

What probably happens is that

  1. Your scene with the slider is created with the sliders default value
  2. You change the value of the slider and the slider remembers it, the important part here is that the value is “held” by the slider so it is a part of the scene
  3. You change your scene to the main menu. While doing so your scene with the slider gets destroyed alongside the slider and the value it’s holding
  4. You go back to the scene and the slider gets created again with its default value

What you need to do is commonly known as “lifting state up” in the hierarchy. In other words you need to save the value of the slider somewhere where it will not get destroyed when the scene changes so you can restore it once you return to the scene with the slider in it.

There are many ways to achieve this. One of them is to create a global script that will store the value of the slider and then check if that value exists in the _ready function of the slider and restore it if necessary.

While this is a catch all it is not always the best or the most convenient, in your case you can probably just get the value from the GlobalWorldEnviroment as Godot is basically doing half of the work for you already. This will also make sure that the values do not get desynchronised.