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