when clicking certain buttons a signal gets sends out

Godot Version

4.4

Question

extends Node

@export var right_press_1 = false
@export var right_press_2 = false
@export var wrong_press = false
signal good
signal bad

func _on_ready() → void:
if right_press_2 == true and right_press_1 == true:
print(“hi”)
emit_signal(“good”)
elif wrong_press == true:
print(“yes”)
emit_signal(“bad”)

func _on_button_right_press() → void:
@warning_ignore(“standalone_expression”)
if right_press_1 == false:
right_press_1 = true
elif right_press_1 == true:
print(“works1”)

func _on_button_2_right_press_2() → void:
@warning_ignore(“standalone_expression”)
if right_press_2 == false:
right_press_2 = true
elif right_press_2 == true:
print(“works2”)

func _on_button_3_wrong() → void:
@warning_ignore(“standalone_expression”)
if wrong_press == false:
wrong_press = true
elif wrong_press == true:
print(“works3”)

func _on_button_4_wrong_press() → void:
@warning_ignore(“standalone_expression”)
if wrong_press ==false:
wrong_press = true
elif wrong_press == true:
print(“works4”)

Can anyone help me with figuring out this code? I want it so that if when I click 2 buttons on my scene it sends out the signal good and when I click one of the other 2 it sends out the signal bad.

Having difficulty understanding exactly what you want - could you explain in a bit more detail?

Do you just want 2 of your buttons to emit ‘good’, and the other 2 to emit ‘bad’?

func _ready() -> void:
   # (you could obviously connect to pressed functions instead)
   button_one.pressed.connect(good.emit)
   button_two.pressed.connect(good.emit)
   button_three.pressed.connect(bad.emit)
   button_four.pressed.connect(bad.emit)

Or are you looking to emit ‘good’ if 2 buttons are pressed simultaneously, and ‘bad’ if only 1 button is pressed?

1 Like

if I press both the right press 1 and the right press 2 good will emit and if I press 1 of the wrong press buttons bad will emit

Without overcomplicating the code, something like that should work. You just need to connect your buttons in the editor to the appropriate exported fields.

signal good
signal bad

@export var button_one: Button
@export var button_two: Button
@export var button_three: Button
@export var button_four: Button

var button_one_pressed: bool
var button_two_pressed: bool

func _ready() -> void:
	button_one.pressed.connect(check_good_press.bind(button_one))
	button_two.pressed.connect(check_good_press.bind(button_two))
	button_three.pressed.connect(bad.emit)
	button_four.pressed.connect(bad.emit)

func check_good_press(button_pressed: Button) -> void:
	if button_pressed == button_one:
		button_one_pressed = true
	elif button_pressed == button_two:
		button_two_pressed = true
	
	if button_one_pressed and button_two_pressed:
		good.emit()
1 Like

The bad signal does get emitted, but the good signal still doesn’t

Ps. I fixed it with a minor change

signal good
signal bad

@export var button_one: Button
@export var button_two: Button
@export var button_three: Button
@export var button_four: Button

var button_one_pressed: bool
var button_two_pressed: bool

func _ready() -> void:
	button_one.pressed.connect(check_good_press.bind(button_one))
	button_two.pressed.connect(check_good_press.bind(button_two))
	button_three.pressed.connect(bad.emit)
	button_four.pressed.connect(bad.emit)

func check_good_press(button_pressed: Button) -> void:
	if button_pressed == button_one:
		button_one_pressed = true
	elif button_pressed == button_one:
		button_two_pressed = true
	
	if button_one_pressed and button_two_pressed:
		good.emit()

just change the elif button_pressed == button_one to: elif button_pressed == button_two

1 Like

True, my mistake, thanks for pointing out. I corrected in my original post as well.