Godot Version
4.6
Question
so im trying to make a button that changes a global variable to true or false, and i want to specify which global variable to change using an export variable. is that possible? if yes, how do you do that?
4.6
so im trying to make a button that changes a global variable to true or false, and i want to specify which global variable to change using an export variable. is that possible? if yes, how do you do that?
Yes, but why? It seems a little overengineered, so if you could explain what you’re trying to achieve, we might be able to suggest a proper way of doing it.
You can use set() method for that.
E.g.
@export var property_to_change: StringName = "test_property"
func _ready() -> void:
# These 2 lines are equivalent:
Globals.set(property_to_change, "test_value")
Globals.test_property = "test_value"
You could add a string as an argument to the button press signal and then use set() with the string and !the_bool to set the variable of that name to opposite of what it was before the press.
thanks! that worked.
i was trying to make a settings menu using custom made buttons
One way to do this is to add metadata to the button. For example, add metadata named what_to_change(type String). Then set the metadata to the button and check the metadata in the button’s pressed handler:
func _on_button_pressed() -> void:
match get_meta("what_to_change"):
"foo":
global_variable_foo = 42
"bar":
something_else = 6
But I would do that only if there were a lot of buttons and they all need to share some common code. For example they all call some function and I can set the function’s parameter value using the metadata. In most cases it is much better just to do separate pressed signal handlers to each button.