Bug with configfile and keybinds

3.5

Hi all! I’m trying to quickly finish up this game I should have finished ages ago, so I’m still in 3.5. However, I had a question, as this problem is stumping me.

I’m using a congfile to save user preferences, but struggling to load the correct keybind in there. For example, my default preference key has attack saves as “Z” but when I try to override it in code with a key like, for example, “G”, the key saves correctly to my file, but does not load correctly. It still loads as G.

Here is the code on my game game settings manager script

extends Node


var save_path = "user://Saves/settings_file.cgf"
#ATM i set it so settings are saved accross ALL games. may change later
var config = ConfigFile.new()
var load_response = config.load(save_path)


#where is value coming from. 
func save_value(section, key, value):
	config.set_value(section, key, value) #why is this value null?
	config.save(save_path)

func load_value(section, key):
	var value_to_pass
	#THIS is where our value is wong number 
	#value to pass is right, value is wrong! for keybinds
	value_to_pass = config.get_value(section, key) #defaultwill return null if not saved correctly
	return value_to_pass

which has load value called by my other script, keybinds manager :

extends Node

var inventory_value = InputMap.get_action_list("INVENTORY")[0]
var interact_value = InputMap.get_action_list("INTERACT")[0]
var attack_value = InputMap.get_action_list("ATTACK")[0]
var pause_value = InputMap.get_action_list("PAUSE")[0]
enum ACTIONS {INVENTORY, INTEREACT, ATTACK, PAUSE}
var current_action = ACTIONS.ATTACK #Arbitrarily chose one
var current_bind = 0 #not using enums for this one. just ints
var string_to_save = null 

# Called when the node enters the scene tree for the first time.
func _ready():
	load_previous_binds()
	print ("from ready on keybinds manager, attack value is ")
	print (attack_value) #so it IS bein set null

#set current keycodes to be correct
func set_correct_keycode(key_name, int_value):
	var new_key = InputEventKey.new()
	#OH I think this one needs the non int value too. th strin value
	(new_key as InputEventKey).set_physical_scancode(int_value)
	InputMap.action_erase_events(key_name)
	InputMap.action_add_event(key_name, new_key)


#this is bein called twice :C
func load_previous_binds():
#so we SAVED te readable keycode string right
#so now we need to lOAD it right
	inventory_value = GameSettingsManager.load_value("Keybinds", "INVENTORY")
	interact_value = GameSettingsManager.load_value("Keybinds", "INTERACT")
	attack_value = GameSettingsManager.load_value("Keybinds", "ATTACK")
	pause_value = GameSettingsManager.load_value("Keybinds", "PAUSE")

# need to update current action 
func accept_action(action):
	current_action = action


func accept_bind_as_int(bind):
	current_bind = bind
	
func accept_string_to_save(string):
	if string != null:
		string_to_save = string


func get_current_binds(current_action, int_bind_value, correct_key_string):
	for i in ACTIONS:
		match current_action:
			"INVENTORY": 
				set_correct_keycode("INVENTORY", int_bind_value)
				GameSettingsManager.save_value("Keybinds", "INVENTORY", correct_key_string)
			"ATTACK":
				set_correct_keycode("ATTACK", int_bind_value)
				GameSettingsManager.save_value("Keybinds", "ATTACK", correct_key_string)
			"INTERACT":
				set_correct_keycode("INTERACT", int_bind_value)
				GameSettingsManager.save_value("Keybinds", "INTERACT", correct_key_string)
			"PAUSE":
				set_correct_keycode("PAUSE", int_bind_value)
				GameSettingsManager.save_value("Keybinds", "PAUSE", correct_key_string)
	
func save_current_binds():
	for i in ACTIONS.size():
		get_current_binds(current_action, current_bind, string_to_save)

I can tell I’m doing something wrong, but i am not sure what i did do that’s wrong.

also, here’s the output for the config file:

[Volume]

"Music Volume"=0.0

[Keybinds]

ATTACK="G"

[DifficultyMods]

EnemyHealth=0.66
EnemyChargeSpeed=1.0
EnemyAttackSpeed=1.0
PlayerDamTakenMod=0.5
PlayerKnockback=0.75

Any help would be greatly appreciated. Thanks for readin :slight_smile:

It looks like you’re storing the scancode of the keybind as a string, but you should save it as an int since physical scan code is an int. I don’t think setting the scancode is enough though, you also need to specify if the key is pressed or not too.

However, since you can just save the entire inputevent in the config file, you should just do that so all the information is saved.

I will try changing it back to int, I had it as an int earlier but somethin else must have went wrong then.

I don’t understand what you mean when you say specify if key is pressed though. Do you mean to make the action actually happen in game? or to rebind the key when it is loaded in?

sorry for my confusion

InputEventKey has a pressed variable. It can be either true or false in order to detect key presses and releases. So it’s extra information that should be set on the keybind.

I know it has a pressed variable, I’m just having a hard time understanding how it corresponds to a config file. Does it need to be marked as pressed to load right, or something?

No, I was just giving it as an example of an additional variable in the class. I think you should just save the entire InputEvent to the config file and then load the entire thing back to make things easier.

oh okay. thank you for advice.

hi thank you for your advice. I’ve saved the input event to config file, but I am havin trouble loading it back in - do you have any tips?

sorry for being dense

You can just use the load_value function and pass in the value to InputMap.action_add_event function

okay. I thought I did that but i’ll double check my code soon .
ty again for all your patience

edit actually id ont think i did that yet. I’ll check it out soon though and try to implement

thanks again for all the help :3 labels are loading in right now, along with keybinds. I Just need to smooth out a few other problems ;p but htats for me to deal with :3

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.