Gdscript to create button with label ignores pressed

Godot Version

4.3

I am new at Godot and expect I am making a dumb error. I have a Node2D scene. Here is the _ready function
func _ready():
print(“ready speeches”)
# connect to signals sent from main scene:
main_node.connect(“toggle_game_rest”, Callable(self, “_on_game_rest”))
main_node.connect(“toggle_game_usedin”, Callable(self, “_on_game_usedin”))
# this sets the initial rest positions of the speeches
var speech_init_pos = Vector2(
get_viewport().get_size().x / 2 - 4 * speech_delta_x,
get_viewport().get_size().y - 2 * speech_delta_y)
#print(str(speech_init_pos))
#for i in range(len(Globals.array2D_speech_title)):
var i = 0
#for key in Globals.dict_speech_title.keys():
for key in Globals.dict_speeches.keys():
var button = Button.new()
# use which_config to specify rest position or usedin position
var which_config = 1
button.name = key
button.set_size(Vector2(80, 50))
#button.text = Globals.dict_speeches[key][0]
button.position = speech_position(
which_config,
speech_init_pos.x,
speech_init_pos.y,
speech_delta_x,
i)
i += 1
# update global position and remember:
Globals.speech_rest_pos[key] = button.position
# button stylebox color:
var style_box = StyleBoxFlat.new()
style_box.bg_color = Globals.background_rgb
style_box.border_color = Globals.background_rgb
button.add_theme_stylebox_override(“normal”, style_box)
# create a label for the button:
var label = Label.new()
label.label_settings = speeches_label_settings
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
label.text = Globals.dict_speeches[key][0]
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
label.size = Vector2(80, 50)
label.clip_text = true
button.add_child(label)
# Connect the input event
button.connect(“pressed”, Callable(self, “_on_button_pressed”))
# finally:
add_child(button)
#
# use _ready to compute the usedIn positions
# for now default to rest positions
Globals.speech_usedin_pos = Globals.speech_rest_pos

Works fine BUT when I click a button, nothing happens. The _on_button_pressed function does not fire. But the button does change color when the mouse enters it so something works.

func _on_button_pressed(button_text: String):
print(button_text + " was clicked!")

Help definitely appreciated.

You should be getting an error
emit_signalp: Error calling from signal ‘pressed’ to callable: ‘xxx::_on_button_pressed’: Method expected 1 arguments, but called with 0.

In order to connect to the built in signals, the function definition must match.
The pressed() signal does not have a parameter.

You can change the connection around like so:
button.pressed.connect(_on_button_pressed.bind(button.text))

Please use the </> to wrap your posted code in code tags thereby keeping formatting.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.