Godot is unable to write to user://

Godot Version

4.3

Question

When I run the load_data() function below, I receive an error: "invalid access to property or key ‘bacgkround’ on a base object of type ‘null instance’.

I receive another error stating “data_serializer.gd:15 @ load_data(): Failed loading resource: user://save.tres. Make sure resources have been imported by opening the project in the editor at least once.”

No file named save.tres is being created in the default save location. It seems the project is unable to create a new folder in user://. Any ideas on how to fix?

extends Resource

const save_path = “user://save.tres”

#list of all save files
@export var background : bg_color = bg_color.new()
@export var save_name : saved_name = saved_name.new()

#save and load functions
func save_data():
ResourceSaver.save(self, save_path)

func load_data():
var save_data_for_load = ResourceLoader.load(save_path, “”)
background = save_data_for_load.backg

@Ogskive I cannot reproduce your error.
edit: I am using this :point_down: and it is working properly

func _ready() -> void:
	ResourceSaver.save(ResourceLoader.load("res://icon.png"),"user://save.tres")
	$TextureRect.texture= (ResourceLoader.load("user://save.tres"))

edit 2: please make sure you are calling function to save resource

ResourceSaver.save returns an Error, you can print the result to have more info about why it is failing.

var error : Error = ResourceSaver.save(self, save_path)
if error != OK:
   print("Error saving file: ", error)

I figured it out! Though embarrassing, it seems I just never actually called the save_data() function, so no save file was ever being created. Setting up the save_data() before the load_data() has fixed the issue.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.