First line of code is being read before the second line

Godot Version

4.2

Question

Hello, I am new in Gadot and am asking if you can combine two lines of code to be read simultaneously.

extends Node2D
@onready var button = $Button
@onready var math = $BubbleEnemy/Math as math
@onready var math2 = $BubbleEnemy2/Math as math
func _ready():
	button.pressed.connect(math._on_button_pressed)
	button.pressed.connect(math2._on_button_pressed)

My problem here is that

button.pressed.connect(math._on_button_pressed)

is being read first while

button.pressed.connect(math2._on_button_pressed)

is not being read or not working cause when I input a correct answer the second one does not get cleared.

extends Node2D
class_name math
@onready var input_num = %inputNum
var math_instance = null
var operand1 = 0
var operand2 = 0
var operation = 0
var correctAnswer = 0
func _ready():
	generate_question()
func generate_question():
	operand1 = randi() % 10 + 1 # Random number between 1 and 10
	operand2 = randi() % 10 + 1 # Random number between 1 and 10
	operation = randi_range(1, 2) # 1: Addition, 2: Subtraction
	if operation == 1:
		correctAnswer = operand1 + operand2
		$Label.text = str(operand1) + " + " + str(operand2)
	else:
		# Ensure non-negative results
		while operand1 < operand2:
			operand1 = randi() % 10 + 1
			operand2 = randi() % 10 + 1
		correctAnswer = operand1 - operand2
		$Label.text = str(operand1) + " - " + str(operand2)
func check_answer():
	var lineEdit = get_tree().get_first_node_in_group("inputNum")
	var playerAnswer = lineEdit.text.strip_edges() # Get player's answer
	var intAnswer = playerAnswer.to_int()
	if intAnswer != null:
		if intAnswer == correctAnswer:
			print("Correct!")
			queue_free()  # Delete Math Question
	lineEdit.text = ""  # Clear the LineEdit after processing the answer
func _on_button_pressed():
	print("Button Pressed")
	check_answer()

Here is the code for what happens when I press the button.

I tried switching

to be like

extends Node2D
@onready var button = $Button
@onready var math = $BubbleEnemy/Math as math
@onready var math2 = $BubbleEnemy2/Math as math
func _ready():
	button.pressed.connect(math2._on_button_pressed)
	button.pressed.connect(math._on_button_pressed)

but the result is the same the difference is

button.pressed.connect(math2._on_button_pressed)

is being read first and being cleared while the other one is not being cleared when I inputed the right answer

Also sorry I did not see that you can preformat the code.

Edit:

It seems I assumed it wrong.
Please disregard the answer below, you can indeed connect multiple functions to signals.


Old answer:

You can only have a single connected function at a time.
So when you connect the second function, you lose the reference to the first one.

The solution is to create a local functions that calls both and connect that instead, so something like this:

extends Node2D
@onready var button = $Button
@onready var math = $BubbleEnemy/Math as math
@onready var math2 = $BubbleEnemy2/Math as math

func _ready():
    button.pressed.connect(call_buttons)

func call_buttons():
    math._on_button_pressed()
    math2._on_button_pressed()

Yeah still encountering the same problem

math._on_button_pressed()

is still being read first even though I’m answering the

math2._on_button_pressed()

part and it also not being cleared

why do you need both to be called at the same time?

because the two random math questions are connected to one button

Would it really matter if one is called first?

Just to clear up some confusion, this is not true:

It is perfectly fine to connect multiple functions to the same signals. So this is not the error.

2 Likes

Yes because i cannot solve the second one without solving the first one

You could use a delta function so it runs every second. For example

 func process(delta):
button.pressed.connect(math._on_button_pressed)
button.pressed.connect(math2._on_button_pressed)

that way it doesn’t run the code at the same time but it does it nearly the same time

so you would just solve the first one, store the answer to a variable then solve the second one.

I’m not sure if this works by the way

No, that’s not a good idea. You can’t connect a signal more than once. Also the code doesn’t need to be run every frame.

2 Likes

Depending on how your scene is set up, could this be the problem?

You’re only ever referencing the first node in the group, never any others.

As far as your exact question: if you uploaded your project, I/others could download and quickly find the problem. It’s possible the problem is that you’re using lowercase “math” for both the class name and your variable name and that could be confusing the compiler? (ps: I strongly advise to use lowercase for variable names and camelcase for class names, i.e. change “@onready var math = $BubbleEnemy/Math as math” to “@onready var math = $BubbleEnemy/Math as Math”. This is the accepted convention in gdscript so not only is that clearer for you, but also easiest for anyone else reading your code to follow.)

With that said, I’d also strongly recommend to not rely on signal order (and always assume that signals can be executed in any order). Instead, I’d listen to the signal in one place that then call the 2 math objects’ functions in the needed order:

func _ready():
     button.pressed.connect(_on_button_pressed)

func _on_button_pressed():
     var result = math.my_awesome_function()
     math2.m_awesome_function(result)

It’s clear, it’s reliable, and your app won’t break if the implementation of signals under the hood ever changes. (The engine doesn’t guarantee the order, afaik, just that both connected functions will be called.)

Hope this helps!

1 Like