How keep button status pressed after screen changed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By yedi
:warning: Old Version Published before Godot 3 was released.

Hi All.

  1. I have a project with 5 scenes.
  2. I use singleton logic to change screens
    http://docs.godotengine.org/en/latest/tutorials/step_by_step/singletons_autoload.html
  3. I have menu bar scene, as a child, on each screen
  4. The menu bar has button “Mode” with Toggle mode selected (in editor)
  5. If I select button on one screen (button is visible selected)
    and then go to next screen the button return to the default status.

Question
Is the any way keep button pressed when I go from one screen to another, by using standard button setting ? or I have to come up with custom logic to keep button pressed all the time?

A quick and easy way would be the one you said.

Your ui should be used only for presentation, your logic and data should be separate from there visual appearance.

So you need to make a variable in a Singleton script and set/unset the variable according to the button press.

An example for this is sound toggle button which should retain it’s state.

You can make the sound button as a packed scene with a script in which it checks the global variable is status and sets it’s presentation inside the _ready()
This way it is as simple as instancing the scene and it will work everywhere.

A nice addition: Make a mute/unmute method in the Singleton and emit a signal when the variable changes. You can listen for that signal on any script. So you can make different sound toggle designs and all will be synced.

vinod | 2016-09-20 04:09

It looks like you want to preserve your GUI across scenes. If you use change_scene, I would suggest you use sub-scene change instead. Take a look at option 3) from my answer here: Scene switching and returning? - Archive - Godot Forum
Note you don’t necessarily need a singleton.
Other options can be interesting too, as you are dealing with scene changing, I guess.

Zylann | 2016-09-20 11:47

Guys, appreciate you answers
Thank you

yedi | 2016-09-21 03:17

:bust_in_silhouette: Reply From: yedi

Here is my solution, how to preserve button (pressed or not pressed ) status across a scenes.
Solution absolutely generic you can copy and paste this script to any of your scene script.
Here are two part of script: one inside func _ready() and second part is func _some_button_toggled(toggled, button)

You can view full example here Scene script name is _scene_A.gd


func _ready():
#Keep all button status Pressed/depressed in memory
var get_children = get_children()
for ch in get_children: #Loop over all children in this screen 
	if ch.get_type() == "Button":  #only Butons type child
		ch.connect("toggled", self, "_some_button_toggled", [ch]) #connect all button to function "_some_button_toggled"
		var buttonStatusPressed = Globals.get(ch.get_name()) #set Global in Button _toggled function
		if buttonStatusPressed != null:
			ch.set_pressed(buttonStatusPressed) #Everytime when open a screen load button status from Global to a button

func _some_button_toggled(toggled, button):
Globals.set(button.get_name(), toggled)