The function is using previous arguments (not intentional!)

Godot Version

Godot 4.6.2 stable

Question

Hello! I’m trying to code a response system, but it seems to only use arguments used before in runtime. Does anyone know how to fix this? I’ve tried searching, but I haven’t seen any solutions to a problem like this. P.S: I know my code is kind of unoptimized right now, but I’ll go back and fix that soon. All replies are greatly appreciated!


# this first chunk is in a function (which is kind of long, so I won't put the full thing there.)
var buttonsTable = [b1, b2]
		Name.visible = false
		for i in len(buttonsTable):
			player_option1 = table[0] #table- the array where the responses are stored. I've checked the table, and it should work normally.
			player_option2 = table[1]
			buttonsTable[i].text = table[i]
			buttonsTable[i].visible = true

		b1.pressed.connect(any_button_press.bind(player_option1)) 
		b2.pressed.connect(any_button_press.bind(player_option2))
		await type_response_dialogue
		b1.visible = false
		b2.visible = false
		emit_signal("function_finished")
		return 
#the any_button_press function
func any_button_press(_response : String):
	print(_response) #prints nothing or the previous option the player could pick.
	type_response_dialogue.emit(_response)

#the script that receives the signal
func type_response_dialogue(player_response : String):

	if currentStage == "tutorial":
		var npc_response_array = Dialogue_opening_initalized.responses[currentStage][player_response]["dialogue"]
		var npc_name = Dialogue_opening_initalized.responses[currentStage][player_response]["Name"]
		dialogueFunctions_initialized.typeTextand_await(npc_response_array, regularDelay, npc_name)
		await dialogueFunctions_initialized.function_finished
		if player_response == "Agree to play.":
			currentEnemy = "Water Demon (Hesper)"
			emit_signal("tutorial_battle")
			$".".visible = false
		else:
			emit_signal("exit_cutscene")
			$".".visible = false
	
	if currentStage == "scene1":
		$".".visible = true
		var npc_array = Dialogue_opening_initalized.part2Responses[player_response]["dialogue"]#I usually get an error here saying that the previous player_response is not a valid key of the dictionary bla bla bla
		var npc_name = Dialogue_opening_initalized.part2Responses[player_response]["Name"]
		dialogueFunctions_initialized.typeTextand_await(npc_array, regularDelay, npc_name)
	response_finished.emit()

Which exact arguments?

Only the player_response-string or others too?

Try to narrow it down a bit by adding prints and following the process.

Only the player_response variable seems to persist. Any idea why?

I would take a look at what is passed to func any_button_press(_response : String).

It could be that when you decide what to pass as an argument you perform some check before updating the argument, and that you don’t pass the check as the previous argument is already stored. It is difficult to know fir sure without seeing anything else ir knowing more about what you are doing elsewhere

Thanks for replying. My script doesn’t have that much in it, so the only thing that passes anything into any_button_press is the code attached above. There are also no additional if/else/elif/match type checks that would interfere with this thing (well, there is an if check before, but if it failed the check, it wouldn’t run the functions or signals above anyways, and it does run them.)

How did you determine that?

In the any_button_press function about halfway through the script, there’s a print statement for the response. It corresponds to the text of the button (in the furst chunk of code.)

Get rid of awaits.

And something should be done about those names…

b1.pressed.connect(any_button_press.bind(player_option1)) b2.pressed.connect(any_button_press.bind(player_option2))

Do you get any warnings from trying to connect already connected signals to the function? They seem to be inside a function that gets called repeatedly.

emit_signal(“function_finished”)

What does this signal do?

The function any_button_press(_response : String) - why do you have it? You call it with one of two signals, to print the argument and emit a third signal, which is awaited by the calling function before it can progress to emit a fourth signal.

Are these things, as it seems, all in the same script?

If so, i seems very convoluted for no reason i m o. I would simplify. It will be easier to debug

1: Nope, no warnings other than the one mentioned at the end

2: emit_signal(“function_finished”) is for another node that handles cutscenes

3: I have the any_button_press thing so the script waits until you’ve pressed a button and to send the signal to another node

4: It’s not in the same script.

I’m really new to Godot, any advice on how to simplify would be appreciated. Thanks.

I gave you an advice above - get rid of awaits and use regular signal handling functions instead.

forgot to reply to that one, that’s all

thanks