0 votes

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 playerslot
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 slot1 = slotid 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
in Engine by (391 points)
edited by

1 Answer

0 votes

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:

....
by (37 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.