how to simplify this code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By potatobanana

how to simplify this code?
it become way to long there a way to simplify it?

basically i wan to make character selection or loading save/load slot, i make 10 empty dict, if i click slot_1it well check ifplayer_dict_1 empty or not, if empty create new one, if it fill with dict , it will load and start game.

what i want to do is when i click 1 out 10 checkbox, it will check if i have data in player_dict, if my player_dict is empty, creation window will instance, if player_dict not empty i can start game using player_dict data from slot i choose.

func _ready() -> void:
	for checkbox in get_tree().get_nodes_in_group("character_checkbox"):
		checkbox.connect("pressed",self,"checkbox_data",[checkbox.get_name()])

the different between this two statement is
1 -"player_slot1" i have 10 player_slot
2 - PlayerGlobalStat.player_dict_1 also have 10 player_dict_ in autoload
3 - slot_id i want to update it when i click the right slot slot_1 = slot_id 1

this code below just 2 out 10, i need to repeat 8 more, it bcome way to long. i wonder there anything that can make it bit short

func checkbox_data(checkbox):
	if get_node(player_slot_path + checkbox).name == "player_slot1":
		if PlayerGlobalStat.player_dict_1.empty () == true: # if dict in dict_1 empty

			slot_id = 1 # help change dict in player global
			start_button.disabled = true # then close able create button
			create_button.disabled =  false # then open able create button
			delete_button.disabled = true
			get_node("slection_Panel/VBoxContainer/HBoxContainer2/create_delete/create").grab_focus()

		else:

			PlayerGlobalStat.current_player_data = PlayerGlobalStat.player_dict_1
			start_button.disabled = false # then open able create button
			create_button.disabled =  true # then close able create button
			delete_button.disabled = false

	else:
		pass


	if get_node(player_slot_path + checkbox).name == "player_slot2":

		if PlayerGlobalStat.player_dict_2.empty () == true: # if dict in dict_1 empty

			slot_id = 2 # help change dict in player global
			start_button.disabled = true # then close able create button
			create_button.disabled =  false # then open able create button
			delete_button.disabled = true

			get_node("slection_Panel/VBoxContainer/HBoxContainer2/create_delete/create").grab_focus()


		else:

			PlayerGlobalStat.current_player_data = PlayerGlobalStat.player_dict_2

			start_button.disabled = false # then open able create button
			create_button.disabled =  true # then close able create button
			delete_button.disabled = false

	else:
		pass
:bust_in_silhouette: Reply From: tripod

I guess the simplest approach is to do a for i in range(1,10) and use some string formatting for the keys. eg

var slotName = "player_slot%d" % i
var dictName = "player_dict_%d" % i
 if get_node(player_slot_path + checkbox).name == slotName:
        if PlayerGlobalStat[dictName].empty () == true:

....