Quick-Tasks/Quick Tasks/addons/Saving/SaveScript.gd
2025-04-15 11:57:44 -07:00

75 lines
2.2 KiB
GDScript

@tool
extends Node
### the script can't do dynapic arrays, just packed arrays
@export var MainVars: Dictionary = {}
@export var SideVars: Dictionary = {}
## 0 means save to MainVars
## 1 means save to SideVars
func SaveVars(File: String, VarLocation: int):
var TempDictionary: Dictionary = {}
var file = FileAccess.open("user://" + File + ".txt", FileAccess.WRITE)
# Know where to get the variables from.
if VarLocation == 0:
TempDictionary = MainVars.duplicate(true)
else:
TempDictionary = SideVars.duplicate(true)
# Algorithm to write stuff to file.
var values = TempDictionary.values()
for i in TempDictionary:
file.store_line(str(i))
file.store_line("")
for i in values:
if i is int:
file.store_line("Int" + str(i))
elif i is float:
file.store_line("Float" + str(i))
else:
file.store_line(str(i))
file.store_line("")
func LoadVars(File: String, VarLocation: int):
var TempDictionary: Dictionary = {}
var file = FileAccess.open("user://" + File + ".txt", FileAccess.READ)
var line = file.get_line()
# Saving the keys first
while line != "":
TempDictionary[line] = null
line = file.get_line()
# temp values for next part
var TempValues = TempDictionary.keys()
var TempInt = 0
# bufferline
line = file.get_line()
# Save the values now
while line != "":
if line.begins_with("[\""):
# We know this is an array. Start by cleaning.
var array: Array
line = line.replace("[", "")
line = line.replace("]", "")
line = line.replace(",", "")
while line.find("\"") != -1:
# Grab the string and parse it into actual variables.
array.append(line.substr(1, line.find("\"", 1) - 1))
line = line.erase(0, line.find("\"", 1) + 2)
TempDictionary[TempValues[TempInt]] = array
elif line.begins_with("Int"):
# It's an int, do good stuff
TempDictionary[TempValues[TempInt]] = line.substr(3).to_int()
elif line.begins_with("Float"):
# It's a float
TempDictionary[TempValues[TempInt]] = line.substr(5).to_float()
else:
TempDictionary[TempValues[TempInt]] = str(line)
line = file.get_line()
TempInt += 1
if VarLocation == 0:
MainVars = TempDictionary.duplicate(true)
else:
SideVars = TempDictionary.duplicate(true)