The old save and load code will need to be updated to look for the old version 1.2.1 style. If this save version is present the game will need to update to the new method of saving the entire dictionary.
The version check is in the global.gd script. It has been updated from nothing, to version 1.3 for this release
# This function will save the game. It creates a dictionary to store all the variables in, and then writes it to a JSON file.
# If the function is unable to open the file for any reason, it will return without saving instead of crashing the game
# After the file is written to, this function will close it to avoid any corruption issues
func saveGame():
# Create a dictionary for the save game to use
var gameData = {
sgTotalGamesPlayed = 0,
sgTotalEasyGamesPlayed = 0,
sgTotalMediumGamesPlayed = 0,
sgTotalHardGamesPlayed = 0,
sgTotalGamesWon = 0,
sgTotalEasyGamesWon = 0,
sgTotalMediumGamesWon = 0,
sgTotalHardGamesWon = 0,
sgTotalTime = 0
}
# Update the dictionary
gameData.sgTotalGamesPlayed = stats.getTotalGamesPlayed()
gameData.sgTotalEasyGamesPlayed = stats.getTotalEasyGamesPlayed()
gameData.sgTotalMediumGamesPlayed = stats.getTotalMediumGamesPlayed()
gameData.sgTotalHardGamesPlayed = stats.getTotalHardGamesPlayed()
gameData.sgTotalGamesWon = stats.getTotalGamesWon()
gameData.sgTotalEasyGamesWon = stats.getTotalEasyGamesWon()
gameData.sgTotalMediumGamesWon = stats.getTotalMediumGamesWon()
gameData.sgTotalHardGamesWon = stats.getTotalHardGamesWon()
gameData.sgTotalTime = stats.getTotalTime()
# Check for issues opening the file
var gameFile = File.new()
if gameFile.open_encrypted_with_pass("user://LightsOutData.sav", File.WRITE, "ThisPassWordIsTheBest") != 0:
return
# Convert file to JSON and save it
gameFile.store_line(gameData.to_json())
gameFile.close()
# This function will load the game from the save file and write all the data into the variables for the stats page
# If the function is unable to open the file for any reason, it will return without loading instead of crashing the game
func loadGame():
# Check if the game file exists
var gameFile = File.new()
if not gameFile.file_exists("user://LightsOutData.sav"):
return
# Check for issues opening the file
if gameFile.open_encrypted_with_pass("user://LightsOutData.sav", File.READ, "ThisPassWordIsTheBest") != 0:
return
# Load the JSON into a dictionry
var gameData = {}
gameData.parse_json(gameFile.get_line())
stats.setTotalGamesPlayed(gameData.sgTotalGamesPlayed)
stats.setTotalEasyGamesPlayed(gameData.sgTotalEasyGamesPlayed)
stats.setTotalMediumGamesPlayed(gameData.sgTotalMediumGamesPlayed)
stats.setTotalHardGamesPlayed(gameData.sgTotalHardGamesPlayed)
stats.setTotalGamesWon(gameData.sgTotalGamesWon)
stats.setTotalEasyGamesWon(gameData.sgTotalEasyGamesWon)
stats.setTotalMediumGamesWon(gameData.sgTotalMediumGamesWon)
stats.setTotalHardGamesWon(gameData.sgTotalHardGamesWon)
stats.setTotalTime(gameData.sgTotalTime)
gameFile.close()