Godot Version
4.4
Question
Hi. I’ve just implemented my very first, rudimentary save system that allows players to write and save journal entries. My goal is for players to be able write and save as many entries as they want, and be able to view all past entries. At the moment, I have a system that allows players to write and view a new entry in a Response scene, save the entry and then view it in the Entries scene. However, as soon as they save a new entry, it simply replaces the entry in the Entries scene. The Save/Load logic is handled in it’s own scene. What is the best way (clean and scalable) to implement a system that allows for multiple saved entries? Would I need to instantiate a new scene for each entry and how would I go about that? Please note that the Load button is there mostly for testing, and I will probably hide or eliminate it once I release the playable build. The load() function is called in _ready() for the purposes of the playable build.
Here is my code for handling saving, loading, and displaying entries.
In Response scene:
extends Node2D
func _ready() -> void:
_display_save()
print("saved data: ", Save.save_dict.question, Save.save_dict.response, Save.save_dict.query, Save.save_dict.reflections)
print("globals: ", Globals.question, Globals.response, Globals.query, Globals.reflections)
#eventually move this to new scene for past entries
#_display_save()
func _display_save():
$Question/QuestionSym.text = Save.save_dict.question
$Reflections/AnswerSym.text = Save.save_dict.response
$"Question/Q Display".text = Save.save_dict.query
$"Reflections/R Display".text = Save.save_dict.reflections
func _on_save_pressed():
Save.save_dict.question = Globals.question
Save.save_dict.response = Globals.response
Save.save_dict.query = Globals.query
Save.save_dict.reflections = Globals.reflections
Save._save()
func _on_load_pressed():
#here for testing, will probably hide/eliminate on release
Save._load()
Globals.question = Save.save_dict.question
Globals.response = Save.save_dict.response
Globals.query = Save.save_dict.query
Globals.reflections = Save.save_dict.reflections
In Save scene:
extends Node2D
const save_location = "user://SaveFile.json"
var question = Globals.question
var response = Globals.response
var query = Globals.query
var reflections = Globals.reflections
var save_dict: Dictionary = {
#"filename" : get_scene_file_path(),
#"parent" : get_parent().get_path(),
"question" : question,
"response" : response,
"query" : query,
"reflections" : reflections
}
func _ready():
_load()
func _save():
print("saved")
var file = FileAccess.open(save_location, FileAccess.WRITE)
file.store_var(save_dict.duplicate())
file.close()
func _load():
print("load")
if FileAccess.file_exists(save_location):
var file = FileAccess.open(save_location, FileAccess.READ)
var data = file.get_var()
file.close()
var save_data = data.duplicate()
save_dict.question = save_data.question
save_dict.response = save_data.response
save_dict.query = save_data.query
save_dict.reflections = save_data.reflections
print("saved data: ", save_data.question, save_data.response,save_data.query, save_data.reflections)
In Entries scene:
extends Node2D
func _ready() -> void:
_display_save()
print("saved data: ", Save.save_dict.question, Save.save_dict.response, Save.save_dict.query, Save.save_dict.reflections)
print("globals: ", Globals.question, Globals.response, Globals.query, Globals.reflections)
#eventually move this to new scene for past entries
#_display_save()
func _display_save():
$Question/QuestionSym.text = Save.save_dict.question
$Reflections/AnswerSym.text = Save.save_dict.response
$"Question/Q Display".text = Save.save_dict.query
$"Reflections/R Display".text = Save.save_dict.reflections
func _input(_event: InputEvent) -> void:
if Input.is_action_pressed("toggle scene"):
get_tree().change_scene_to_file("res://response.tscn")
pass
Global Scene:
extends Node
var question: String
var response: String
var query: String
var reflections: String
Please let me know if there is any other information I should be sharing. The player input for the entries is handled in via LineEdit nodes (Query, Reflections) both from a pop up window in the Response scene, and well as 2 LineEdits (Question, Answer) from the main scene. Happy to answer questions or clarify anything as needed! Thanks in advance for any support!