0 votes

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 SAVEDIR = "user://defineddir/"

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 = SAVEDIR + "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 onbackpressed():
emitsignal("backpressed")

func onsaveone_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 onsavetwo_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 onsavethree_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")
Godot version 3.4.2
in Engine by (14 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.