Game crashes when I attempt to rebind buttons

Recently, I’ve been working on a solo indie project, and to implement control rebinding I followed a tutorial by The Coffee Crow ( https://www.youtube.com/watch?v=vNQBFn0faws ) and I’ve run into a bug where whenever I run the game and try to rebind any of the controls, the game crashes, I will put my code bellow and give as much information as I can.
`class_name HotkeyRebindButton
extends Control

@onready var label: Label = $HBoxContainer/Label as Label
@onready var button: Button = $HBoxContainer/Button as Button

@export var action_name : String = “Walk_left”

func _ready() → void:
set_process_unhandled_key_input(false)
set_action_name()
set_text_for_key()

func set_action_name() → void:
label.text = “Unassigned”
# This names the buttons in the controls menu
match action_name:
“Walk_left”:
label.text = “Walk Left”
“Walk_rigth”:
label.text = “Walk Rigth”
“Jump”:
label.text = “Jump”

func set_text_for_key() → void:
var action_events = InputMap.action_get_events(action_name)
var action_event = action_events[0]
var action_keycode = OS.get_keycode_string(action_event.physical_keycode)

button.text = "%s" % action_keycode

func _on_button_toggled(button_pressed):
if button_pressed:
button.text = “Press any key…”
set_process_unhandled_input(button_pressed)

	for i in get_tree().get_nodes_in_group("hotkey_button"):
		if i.action_name != self.action_name:
			i.button.toggle_mode = true
			i.set_process_unhandled_input(false)

else:
	for i in get_tree().get_nodes_in_group("hotkey_button"):
		if i.action_name != self.action_name:
			i.button.toggle_mode = false
			i.set_process_unhandled_input(false)
	
	set_text_for_key()

func _unhandled_key_input(event):
rebind_action_key(event)
button.button_pressed = false

func rebind_action_key(event) → void:
InputMap.action_erase_events(action_name)
InputMap.action_add_event(action_name, event)

set_process_unhandled_key_input(false)
set_text_for_key()
set_action_name()

`

the console tells me the error is
"E 0:00:03:171 HotkeyRebindButton._on_button_toggled: Invalid access to property or key ‘action_name’ on a base object of type ‘Button’.
hotkey_rebind_button.gd:39 @ HotkeyRebindButton._on_button_toggled()
hotkey_rebind_button.gd:39 @ _on_button_toggled() "
and the hotkey_button group said in the tutorial is in the “HotKeyRebindButton” control node as a global group, if anyone could explain what is the error I would be extremely thankfull as I am really really new to not just godot but programing in general. Thank you in advance!

Place this before the first if statement in the for loop:

if not ("action_name" in i): continue

Basically, don’t try to access action name if the object doesn’t have it.

where exactly should I place it? I tried placing it where I thought it should be and it sort of worked, but it won’t allow for the selection of a different key, really sorry to bother I started literally last week, again thank you so much

Needs to go in both loops:

If that’s breaking something else, post details about that problem :slight_smile:

Don’t apologize for your lack of experience. That matters far less than your willingness to try things, and your willingness to ask questions when you get stuck.

so, when I add this, it isn’t crashing anymore, but the part that is supposed to prevent you from selecting multiple options and the part that rebinds the action isn’t working anymore. I have no idea why

Would you be so kind as to post those specific parts?

Please put the code in a preformatted text box. Makes it much easier to read.

here are the parts func _unhandled_key_input(event): rebind_action_key(event) button.button_pressed = false

func rebind_action_key(event) -> void: InputMap.action_erase_events(action_name) InputMap.action_add_event(action_name, event)

set_process_unhandled_key_input(false)
set_text_for_key()
set_action_name()`
func set_text_for_key() → void:
  var action_events = InputMap.action_get_events(action_name)
  var action_event = action_events[0]
  var action_keycode = OS.get_keycode_string(action_event.physical_keycode)

  button.text = "%s" % action_keycode

You are calling this before you set the action name. I suspect the event is being bound to the old action.

How would I fix that? Should I just move this part of the code lower?

Reverse the calls.

I may be wrong though. Reading set_action_name() this morning (with adequate caffeine in my system) I notice that it doesn’t actually do what it says on the tin - it doesn’t set the action_name variable.

Since most of your logic uses that variable, not setting it to a new value could be at the root of your issue.