Topic was automatically imported from the old Question2Answer platform.
Asked By
TraitPhoe
I’m trying to make a simple ax^2 + bx + c calculator in Godot which will tell an answer by pressing a button int the interface, but when I press the button I get an error:
E 0:00:02.160 emit_signal: Error calling method from signal ‘pressed’: 'Node2D(Calculator.gd)::_on_Button_pressed': Method expected 1 arguments, but called with 0..
Here is the code
func _on_LineA_text_entered(A):
var a = A
print("a=", A)
func _on_LineB_text_entered(B):
var b = B
print("b=", B)
func _on_LineC_text_entered(C):
var c = C
print("c=", C)
func _on_Button_pressed(a, b, c):
var x1 = a + b + c
print(x1)
I’d hazard a guess that _on_Button_pressed() should have no arguments if it’s being called directly from an actual button control. You need to store a, b and c in top-level vars, or get them from the LineEdit controls, in order to use them inside _on_Button_pressed().
The pressing of the button fires off _on_Button_pressed, but doesn’t pass arguments at all which is why your error says that. Refactor your code so that _on_Button_pressed doesn’t need args, but instead works with local vars set by your other functions.
Now I understand but I don’t know how to change variables on top with local variables in functions. If I type
var a = 0
func _on_LineA_text_entered(A):
var a = A
func _on_Button_pressed():
var x1 = a
print(x1)
It will still print “0” and not whatever you typed in LineEdit
TraitPhoe | 2023-02-09 16:24
Easy fix, see in the _on_LineA_text_entered() you’re creating a new a, what’s called a local variable. It will only have the value you give it within that function, and will be destroyed afterwards. Change the line var a = A to a = A and you’ll be using the one from the top of the file instead.
cassie | 2023-02-09 16:32
Thanks! It worked and counts discriminant with x1 and x2 very well. I’ll make it more flexible and polish it later