Help with sliming down code into some functions and arguments

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

Hey Everyone,

I wondered if someone could take a look at this and advise how I would go about perhaps cutting the code down into more functions with arguments. As I am trying not to repeat code in my GD scripts.

Cheers
Jay

CODE BELOW

   extends Node

signal back_pressed

const SAVE_DIR = "user://defined_dir/"

onready var backButton = $MainMenu_MarginContainer/MainMenu_PanelContainer/MainMenu_MarginContainer/MainMenu_VBoxContainer/BackButton

onready var saveOneButton = $MainMenu_MarginContainer/MainMenu_PanelContainer/MainMenu_MarginContainer/MainMenu_VBoxContainer/SaveButtonsHboxTwo/SaveOneButton
onready var saveTwoButton  = $MainMenu_MarginContainer/MainMenu_PanelContainer/MainMenu_MarginContainer/MainMenu_VBoxContainer/SaveButtonsHboxTwo/SaveTwoButton

onready var saveThreeButton = $MainMenu_MarginContainer/MainMenu_PanelContainer/MainMenu_MarginContainer/MainMenu_VBoxContainer/SaveButtonsHboxTwo/SaveThreeButton

onready var saveOnePanel = $MainMenu_MarginContainer/MainMenu_PanelContainer/MainMenu_MarginContainer/MainMenu_VBoxContainer/SaveButtonsHboxOne/SaveOnePanel

onready var saveTwoPanel = $MainMenu_MarginContainer/MainMenu_PanelContainer/MainMenu_MarginContainer/MainMenu_VBoxContainer/SaveButtonsHboxOne/SaveTwoPanel

onready var saveThreePanel = $MainMenu_MarginContainer/MainMenu_PanelContainer/MainMenu_MarginContainer/MainMenu_VBoxContainer/SaveButtonsHboxOne/SaveThreePanel

var savePathOne = SAVE_DIR + "saveOne.dat"
var savePathTwo = SAVE_DIR + "saveTwo.dat"
var savePathThree = SAVE_DIR + "saveThree.dat"

var fileSaveOne = File.new()
var fileSaveTwo = File.new()
var fileSaveThree = File.new()

func _ready():

backButton.connect("pressed", self, "on_back_pressed")
saveOneButton.connect("pressed", self, "on_save_one_pressed")
saveTwoButton.connect("pressed", self, "on_save_two_pressed")
saveThreeButton.connect("pressed", self, "on_save_three_pressed")

if (fileSaveOne.file_exists(savePathOne)):
	var viewError1 = fileSaveOne.open_encrypted_with_pass(savePathOne, File.READ, "passwordhidden")
	if (viewError1 == OK):
		var saveDataOne = fileSaveOne.get_var()
		saveOnePanel.text = str("Total Lives: " , saveDataOne.values()[0], "\n", "Continues: " , saveDataOne.values()[1], "\n", "Current Level: " , saveDataOne.values()[2])
if (fileSaveTwo.file_exists(savePathTwo)):
	var viewError2 = fileSaveTwo.open_encrypted_with_pass(savePathTwo, File.READ, "passwordhidden")
	if (viewError2 == OK):
		var saveDataTwo = fileSaveTwo.get_var()
		saveTwoPanel.text = str("Total Lives: " , saveDataTwo.values()[0], "\n", "Continues: " , saveDataTwo.values()[1], "\n", "Current Level: " , saveDataTwo.values()[2])

if (fileSaveThree.file_exists(savePathThree)):
	var viewError3 = fileSaveThree.open_encrypted_with_pass(savePathThree, File.READ, "passwordhidden")
	if (viewError3 == OK):
		var saveDataThree = fileSaveThree.get_var()
		saveThreePanel.text = str("Total Lives: " , saveDataThree.values()[0], "\n", "Continues: " , saveDataThree.values()[1], "\n", "Current Level: " , saveDataThree.values()[2])
	

func on_back_pressed():
emit_signal(“back_pressed”)

func on_save_one_pressed():

	saveOneButton.disabled = true
	
	var dataSaveOne = {
		"total lives" : Helpers.totalLives,
		"total continues" : Helpers.currentContinues,
		"current level" : $"/root/LevelManager".currentLevelIndex
	}
	# Making Directories #
	
	var dir = Directory.new()
	if (!dir.dir_exists(SAVE_DIR)):
		dir.make_dir_recursive(SAVE_DIR)
	
	
	var saveError = fileSaveOne.open_encrypted_with_pass(savePathOne, File.WRITE, "passwordhidden")
	if (saveError == OK):
		fileSaveOne.store_var(dataSaveOne)
		saveOnePanel.text = str("Total Lives: " , dataSaveOne.values()[0], "\n", "Continues: " , dataSaveOne.values()[1], "\n", "Current Level: " , dataSaveOne.values()[2])
		fileSaveOne.close()
	else:
		emit_signal("back_pressed")

func on_save_two_pressed():

saveTwoButton.disabled = true

var dataSaveTwo = {
	"total lives" : Helpers.totalLives,
	"total continues" : Helpers.currentContinues,
	"current level" : $"/root/LevelManager".currentLevelIndex
}
# Making Directories #

var dir = Directory.new()
if (!dir.dir_exists(SAVE_DIR)):
	dir.make_dir_recursive(SAVE_DIR)


var saveError = fileSaveTwo.open_encrypted_with_pass(savePathTwo, File.WRITE, "passwordhidden")
if (saveError == OK):
	fileSaveTwo.store_var(dataSaveTwo)
	saveTwoPanel.text = str("Total Lives: " , dataSaveTwo.values()[0], "\n", "Continues: " , dataSaveTwo.values()[1], "\n", "Current Level: " , dataSaveTwo.values()[2])
	fileSaveTwo.close()
else:
	emit_signal("back_pressed")

func on_save_three_pressed():

saveThreeButton.disabled = true

var dataSaveThree = {
	"total lives" : Helpers.totalLives,
	"total continues" : Helpers.currentContinues,
	"current level" : $"/root/LevelManager".currentLevelIndex
}
# Making Directories #

var dir = Directory.new()
if (!dir.dir_exists(SAVE_DIR)):
	dir.make_dir_recursive(SAVE_DIR)


var saveError = fileSaveThree.open_encrypted_with_pass(savePathThree, File.WRITE, "passwordhidden")
if (saveError == OK):
	fileSaveThree.store_var(dataSaveThree)
	saveThreePanel.text = str("Total Lives: " , dataSaveThree.values()[0], "\n", "Continues: " , dataSaveThree.values()[1], "\n", "Current Level: " , dataSaveThree.values()[2])
	fileSaveThree.close()
else:
	emit_signal("back_pressed")
:bust_in_silhouette: Reply From: MadTinkerer

First of all, you certainly want to combine the code from DataSaveOne_pressed(), DataSaveTwo_pressed(), and DataSaveThree_pressed() into a single SaveData function. Then it’s just a matter of passing fileSaveOne, fileSaveTwo, or fileSaveThree to the DataSave function. Then you can reduce DataSaveOne_pressed() to

func DataSaveOne_pressed():
   SaveData(fileSaveOne)

But if you really want to be efficient, toss out “two” and “three” of everything until you only have “one”. Then change the name of all the "one"s in the code so it’s just the number 1. For example: “save1.dat”. Then, once you’re sure that still works the way you want, make it so that everywhere you define it as 1, you instead create it from a variable. Something like

"save" + str(saveNum) + ".dat"

Where saveNum is an integer.

Then you can do something like

func DataSaveOne_pressed():
   SaveData(1)

func DataSaveTwo_pressed():
   SaveData(2)

func DataSaveThree_pressed():
   SaveData(3)