Save and load system just returns null

Godot Version

4.2

Question

Hi, just no matter how i do it when i try to save my resource it just returns null on loading

I’m making it to export to android but even in just gameview it wont save

i have tried fileacces aswell but with no luck.

Thanks for any help :slight_smile:

class_name folder_Manager
extends Resource


@export var folders : Array[FolderObject] 

func _init():
	print("")

func add_Folder(fo : FolderObject)->void:
	folders.append(fo)

class_name save_Load_Manager
extends Control

const save_Path : String =  "user://Savedata.res"
var fm = folder_Manager.new()

# Called when the node enters the scene tree for the first time.
func _ready():
	verify_save_dir(save_Path)
	
func verify_save_dir(path:String):
	var dir = DirAccess.make_dir_absolute(path)
	if(dir == null): 
		print("null")
	


func _on_button_pressed():
	var a = FolderObject.new("hej", Button.new())
	fm.add_Folder(a)
	ResourceSaver.save(fm, save_Path)


func _on_button_2_pressed():
	fm =ResourceLoader.load(save_Path).duplicate(true)
	print(fm)# Replace with function body.

extends Resource
class_name FolderObject

@export var Cards =
@export var textName : String
var button

func _init(name : String, b : Button):
textName = name
button = b

func add_Card(c : Card):
Cards.append(c)

type or paste code here
1 Like

Couple questions:

  1. Have you tried this code on your computer? Does it work there?
  2. Are you using a debug apk or production apk?
  3. Are you using an emulator or a physical device to test this?
  4. What version of the Android OS are you using? Is the Godot version you’re using compatible with the android version you are using?
  1. yes, it dosn’t work, i just do it in the game view
  2. no clue, i havent tested it on the phone as it dosn’t even work in game view

thanks for your time

1 Like

You are making it a directory, you cannot save a file over a directory. remove this verify_save_dir function.

You can check the error messages you may recieve too


func _on_button_pressed():
	var a = FolderObject.new("hej", Button.new())
	fm.add_Folder(a)
	var err := ResourceSaver.save(fm, save_Path)
	if err != OK:
		push_warning("Failed to save: %s" % error_string(err))
2 Likes

I did your changes and got: Can’t open file, couldn’t unfortunatly search my way as to why it can’t open it.

i did try the other way but in the load function on the "print(folderMana…) i get an error where the object is “encoded” so no luck there either, Thanks for your time:

func loadSave():
	var save_file = FileAccess.open("user://savegame.tres", FileAccess.Read)
	var err := save_file.get_error()
	if(err == OK):
		folderManager = save_file.get_var()
		save_file.close()
		print(folderManager.folders.size())
	else: print(error_string(err) + "H")
	
func save():
	var save_file = FileAccess.open("user://savegame.tres", FileAccess.WRITE)
	var err := save_file.get_error()
	if(err != OK):
		print(error_string(err))
	else:
		var a = FolderObject.new("hej", Button.new())
		folderManager.add_Folder(a)
		save_file.store_var(folderManager)
		save_file.close()
		folderManager = null

store_var may not work for whole resources/objects

1 Like

Thank you, i got it to work with your help

I fixed it by manually adding a “save_file” in the appdata

func _on_button_pressed():
	var a = FolderObject.new("hej", Button.new())
	fm.add_Folder(a)
	fm.add_Folder(a)
	fm.add_Folder(a)
	print(fm.folders.size())
	var err := ResourceSaver.save(fm, save_Path)
	fm = null
	
	if err != OK:
		print(error_string(err))


func _on_button_2_pressed():
	fm = ResourceLoader.load(save_Path)
	print(fm.folders.size())# Replace with function body.

I believe the properties have to be @exported for ResourceSaver to save them, make sure folders is exported

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