Settings saves file even when I dont apply them. (solved)

Godot Version

Godot 3.4

Question

I have noticed that on the settings of my game, when i change a key or mouse sensitivity, it saves even when i click cancel or close Godot. I have no idea what is causing this.

Naming might be poor because i originally made it edit controls before adding mouse sensitivity.

Edit: I found out the reason. It was not the script. I copied the apply button to make the cancel button, so it was connected to the same signal as apply in the signals in the node tab on the top right.

I decided that i would edit instead of delete, in case anyone has similar problems.

PS, I don’t mind if you steal my script consider it public domain.

extends Control

var exe_folder = OS.get_executable_path().get_base_dir()
var file_path = exe_folder.plus_file("settings01.txt")

var settings_dict := {}

func _on_Apply_pressed():
	save_settings()
	get_tree().change_scene("res://Menu.tscn")

func _on_Cancel_pressed():
	get_tree().change_scene("res://Menu.tscn")

func _ready():
	settings_dict = load_settings()
	$HSlider.connect("value_changed", self, "slider", ["sensitivity"])

func load_settings() -> Dictionary:
	var keys = {}
	var file = File.new()
	if file.file_exists(file_path):
		file.open(file_path, file.READ)
		while not file.eof_reached():
			var line = file.get_line().strip_edges()
			if line == "":
				continue
			var parts = line.split(" = ")
			if parts.size() == 2:
				keys[parts[0]] = parts[1]
		if keys.has("sensitivity") and has_node("HSlider"):
			$HSlider.value = int(keys["sensitivity"])
		file.close()
	return keys

func save_settings():
	var file = File.new()
	file.open(file_path, File.WRITE)
	for k in settings_dict.keys():
		file.store_line(k + " = " + str(settings_dict[k]))
	file.close()

func slider(value, property: String):
	settings_dict[property] = int(value)

1 Like