Journal save system w/ multiple saved entries

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!

I’d suggest generally to use a supported version like 4.6 or 4.7 unless you’re deep in development, 4.4 is outdated and you’re likely to run into bugs that won’t be fixed for that version (at minimum when you start your next project)

You’ll need to use Arrays to store more than one value within a variable, currently you are correct it’s just going to overwrite the singular variables you have. If you want to write to different files your save_location will have to change before saving and loading

Can I check for understanding before I start messing around in my project? My understanding/plan (likely to change a bit as I experiment in Godot, and inevitable break and fix things, but this is a gist) is to:

  1. Create an Array variable for each LineEdit that I am taking input from.
  2. Append the LineEdit.text values to it’s appropriate Array on submit.
  3. I would want to display the most recent submission in the Response scene, so I would use negative indexing to grab the last value in the Array, and assign that to the corresponding Label.
  4. I think I want to still save everything to the same save file, so I wouldn’t have to change much in the Save scene, aside from replacing the single variables I have with their corresponding Arrays.
  5. To display the saved entries, I would order the entries from the oldest to newest, so I would just start at the beginning of the Array and use a For Loop to assign a Label to each index value, which I can then use to display each entry. I need to think through how I want to format stuff and if I need to instantiate new scenes in order to have space to display multiple entries, etc., but this is the basic gist.

I’m happy to play around and experiment for my learning process, so I am not looking for answers/solutions from you per se, just confirmation that I am generally heading in the right direction before I start implementing and inevitably start breaking stuff, lol.

Sounds good, didn’t know you could use negative indices- I would personally prefer to use the array.back() function to get the most recently appended value

Good to know. That seems preferable.