Hi. Have tried to look this up but all the info I’ve found just tells me about assigning the buttons to a group which I’ve already done through the inspector.
I have the button group saved as res://enviro.tres The buttons I have currently coded function when toggled after I have entered the scene. What I can’t figure out is how to check for one of the group already being toggled as I enter the scene and have it’s toggle signal used. These toggles set the texture of a TextureRect to a different image depending on which is toggled. I’d like that choice to show on entry to the scene.
What is the actual source of the selection? What decides which button is toggled when the group is loaded?
Normaly you’d have the selection stored apart from the gui layout somewhere. The scene would load that up when it is created and switch the buttons state accordingly, and set the texture in your case. The “pressed” signal is how you know someone pushed the button in the ui, nothing else.
It’s just set via the inspector currently which doesn’t cause it to trigger on entering the scene. There is no code to get which button is toggled when entering because I can’t find any examples with clear syntax apart from ones that just create a group and add buttons to it. Nothing that loops through the group and activates whatever has been toggled.
# Reference your ButtonGroup resource
@export var button_group: ButtonGroup
func check_active_button():
var pressed_button = button_group.get_pressed_button()
if pressed_button:
print("The active button is: ", pressed_button.name)
else:
print("No button is currently active.")
I can’t see why I would need to export the group in though as it’s part of the scene and I assume ButtonGroup on the second line would be the path to the group resource in question. This is the first time I’ve used Godot so still a little confusing.
Well you don’t really “find” what button is toggled if it is just set in the layout, it’s predefined and you just go and activate the texture corresponding to that. I mean, there isn’t a dynamical component here to process via the script logic.
I imagine what you have in mind is to store the last selected texture and restore that whenever the gui is opened. How you save the state is an entire different story, but ultimately you will have some source of “what the current texture is” inside your script. Then what you do is obtain a list of all the buttons in the group, loop through them, and activate the one corresponding to that current texture - these are the pieces you can totally find code for.
I feel you may want to get some example projects like godotengine/godot-demo-projects: Demonstration and Template Projects , and dig through to see how the things are done in general, gui, code and all. Learning by trying does work, but you need some idea of what is going on first.
Yeah I’m wanting to obtain that list of buttons but I can’t find anything that does that. It’s sounding like setting up a group through the editor is completely pointless.
It didn’t. It saved as .tres. I’m not trying to persist anything at this point. At the moment you can enter the menu and use 3 of the 16 buttons in the group to display different textures on a TextureRect. This works fine when you go to the scene and press the buttons. There is an option to have a button start toggled but when entering the scene for the first time this toggled state doesn’t trigger. It only does so when toggled from within the scene. I need something that will read the button states on entering the scene and trigger whichever is toggled. It doesn’t have to persist on leaving the scene. I believe I should be able to code that myself. If I was doing this in Vscode I’d have a struct set up for the buttons and corresponding textures but from what I can make out Godot doesn’t have structs.
That doesn’t work as it also hides everything in the scene including the buttons. I’m eventually going to persist everything anyway but I still need a way to check all the buttons in the group and return which is toggled on. I can’t seem to find a way to do that. I could do a ready function that goes through the buttons one by one using a condition but that means the button functions would essentially be coded twice.
Well you need something to check them and set it. Either each button needs a script to check itself on ready, or you need to have something that iterates through them and checks.
Yeah but I can’t find anything that iterates through the group. That’s pretty much the problem. Any script I have found just assigns buttons to a group one by one. I can’t find any examples of cycling through the group and checking the buttons.
Put this on a node that is above them all in the hierarchy.
func set_button(node: Node) -> void:
if node is Button:
#Your code here for determining if the button is pressed
#Your code here for doing something with it
for subnode in node.get_children():
set_button(subnode)