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
extends Node
signal back_pressed
const SAVE_DIR = "user://defined_dir/"
onready var backButton = $PATH
onready var saveOneButton = $PATH
onready var saveTwoButton = $PATH
onready var saveThreeButton = $PATH
onready var saveOnePanel = $PATH
onready var saveTwoPanel = $PATH
onready var saveThreePanel = $PATH
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")
~~~