Please Help! i'm building a calculator and i don't know how to identify button using a single function

so i’m building a calculator and i don’t want multiple lines of code for the exact same thing. i want to make sure i identify which button is clicked on button click using a single function but i couldn’t find any resource or reference. kindly help me out

extends Sprite2D

var answer = 0
@onready var equation = $Equation
@onready var answerdisplay = $Answer

func _on_all_clear_button_pressed():
equation.text = “”
answerdisplay.text = "= "

func _on_button_1_pressed():
equation.text = equation.text + “1”

func _on_button_2_pressed():
equation.text = equation.text + “2”

func _on_cancel_button_pressed():
equation.text = equation.text.left(-1)

func _on_button_7_pressed():
equation.text = equation.text + “7”

func _on_button_8_pressed():
equation.text = equation.text + “8”

func _on_button_9_pressed():
equation.text = equation.text + “9”

func _on_button_4_pressed():
equation.text = equation.text + “4”

func _on_button_00_pressed():
equation.text = equation.text + “00”

func _on_button_3_pressed():
equation.text = equation.text + “3”

func _on_button_0_pressed():
equation.text = equation.text + “0”

func _on_button_5_pressed():
equation.text = equation.text + “5”

func _on_button_6_pressed():
equation.text = equation.text + “6”

func _on_decimal_button_pressed():
equation.text = equation.text + “.”

func _on_add_button_pressed():
equation.text = equation.text + “+”

func _on_equalsbutton_pressed():
answerdisplay.text = "= " + str(answer)

func _on_divide_pressed():
equation.text = equation.text + “÷”

func _on_percentage_button_pressed():
equation.text = equation.text + “%”

func _on_subtrack_button_pressed():
equation.text = equation.text + “-”

func _on_multiply_button_pressed():
equation.text = equation.text + “*”

func identifywhichbuttonisclicked(): #How can i identify which button activated the function
pass
please let me know what can be done here

Your button can have variable and call calculator function with argument variable

can u please give me an example

Dude I know what variables are, I meant how would we still be able to detect which button was clicked because they all will be tied to the same function. Because in order to set a variable we would still need the access to the button click and if I had that why would I need the variable

for button in button_holder.get_children():
	button.pressed.connect(func(): equation.text=equation.text+button.name)

advanced but will do most of your script

Thanks but that’s not gonna work for what I’m going for, let me explain, I don’t know which button triggered the function so if I loop through the button holder to find my button I need to have a variable to actually look for Inside the buttons and how will I find the variable if I don’t know which button is clicked

then pass it during updating equation.text
like:

button.pressed.connect(func():
	equation.text=equation.text+button.name
	detect_pressed(button)
	)

That’s right! Thanks alot!

with just signals you can limit most of your code to:

func _on_button_pressed(extra_arg_0: String) -> void:
	equation.text = equation.text + extra_arg_0 


but if you don’t like manual work with Signals you put code on Buttons

extends Button

@export var value: String

func _ready() -> void:
	pressed.connect(self._button_pressed)

func _button_pressed():
	var parent = get_parent()
	if(parent.has_method("_on_button_pressed")):
		parent._on_button_pressed(value);

instead export you can use, name, Text, or Meta.
in my version I used Calculator as parent of button but you can get any node with get_node or export

Oh right! Didn’t come to my mind, thanks I was thinking about condensing code

1 Like