Save/Load Error Message

I’m still quite new to using Godot and I just followed a tutorial on how to save/load a game and put basically the same code in my own project but it gave me 2 error messages I don’t really understand. Can anyone tell me what they mean and/or how to fix it?

extends Node

var save_path = "user://loadgame.save"

var scene_collectionbook = preload("res://feesh_scenes/collection_book.tscn")
@export var value = Label


func _on_book_button_pressed():
	get_tree().change_scene_to_packed(scene_collectionbook)
	
	
func save():
	var file = FileAccess.open(save_path, FileAccess.WRITE)
	file.store(value)
	
func load_data():
	if FileAccess.file_exists(save_path):
		var file = FileAccess.open(save_path, FileAccess.READ)
		value.text = file.get_var(value.text)
	else:
		print("No Data Saved")
		value = 0

# Called when the node enters the scene tree for the first time.
func _ready():
	load_data()
	 # Replace with function body.
	
func _process(_delta):
	save()

i didnt find any problem in your code and every thing is right , just you can use store_var instead of store

1 Like

Hello, below is a summary of the common script errors and warnings in your code:

  1. Incorrect Function Name:
    There is no store function in the FileAccess class. It looks like you meant to use store_var.

  2. Parameter Type Mismatch:
    In your load_data function, FileAccess.get_var() expects a Boolean as its first parameter, but you are passing a String. Remember that in a Boolean context, an empty string evaluates to false, while any non-empty string evaluates to true.

  3. Unnecessary Data Storage:
    Your script appears to be saving and loading the entire Label Object stored in value. Since you only need the Label’s text, it’s better to save just the text property rather than the whole Object.

  4. Type Assignment Issue:
    You try to assign an Object to a property of type String when using value.text = file.get_var(value.text). If it’s indented this way, try converting the object to a string first to avoid errors.
    You also try to assign a Integer to a label when there is no data with value = 0. When you’ll try to use value as a label you’ll get an error as it is now an Integer.
    If you want to trigger errors more easily use typed variables.

  5. Future Heavy performance impact on large datasets
    You are saving data every frame within your _process function. Since _process is called on each frame, this approach can severely impact performance. Consider saving data only when necessary, such as in response to a specific event or on a timed interval, instead of doing it every frame.

Tip: In Godot, you can hold Ctrl and click on a method/property to view its description, or use Godot 4.4 where you can simply hover your mouse over a method/property to see its details. You can then check if a method/property exists and know how to properly use it.

Example Code (Storing the whole Label Object):

extends Node

var save_path = "user://loadgame.save"
var scene_collectionbook = preload("res://feesh_scenes/collection_book.tscn")
@export var value = Label

func _on_book_button_pressed():
	get_tree().change_scene_to_packed(scene_collectionbook)
	
func save():
	var file = FileAccess.open(save_path, FileAccess.WRITE)
	file.store_var(value)
	
func load_data():
	if FileAccess.file_exists(save_path):
		var file = FileAccess.open(save_path, FileAccess.READ)
		# As you stored the object, get it back
		value = file.get_var()
	else:
		print("No Data Saved")
		value = null# Or queue free the node.

func _ready():
	load_data()
	
func _process(_delta):
	save()

More Efficient Code (Storing/Loading only the Label text):

extends Node

var save_path = "user://loadgame.save"

var scene_collectionbook = preload("res://feesh_scenes/collection_book.tscn")
@export var value = Label


func _on_book_button_pressed():
	get_tree().change_scene_to_packed(scene_collectionbook)
	
	
func save():
	var file = FileAccess.open(save_path, FileAccess.WRITE)
	# It may not be what you want, check the method description.
	file.store_string(value.text)
	
func load_data():
	if FileAccess.file_exists(save_path):
		var file = FileAccess.open(save_path, FileAccess.READ)
		value.text = file.get_as_text()
	else:
		print("No Data Saved")
		value = null# Or queue free the node.

func _ready():
	load_data()

func _on_game_event_happened():
	save()

When storing and loading data, always verify the integrity of the loaded data to prevent potential errors or bugs.

2 Likes

This works! Thank you so much for your help! I appreciate the explanation!

1 Like