Godot Version
v4.2.1.stable.official [b09f793f5]
Question
I have a 26MB csv file that loads line by line to become values in a Dictionary -this results in a Dictionary of ~310k Resources. I estimate the size in RAM is probably like 200-400MB.
Loading this file every time at game start takes a bloody long time (~20 seconds) so I want to just serialize the dictionary, maybe compress it, then load the serialized dataset so it doesn’t take so long on subsequent loads.
Here’s my current boot code:
func _ready():
if not FileAccess.file_exists("user://dictionary.save"):
#do_the_first_file_parse()
save_to_file("user://dictionary.save")
I’ve attempted saving the file a variety of different ways, using the different tutorials, but most of the simple solutions don’t seem to support the complexity of my resources. Right now when I run my save code (below), it creates a 0 KB dictionary.save file and then returns Error code 15 File Unrecognized. I don’t quite understand why this happens.
I know I was able to save with this filename in this directory using other Godot save code, I just wasn’t able to load effectively after creating the file using the earlier approaches. Any help would be appreciated!
Here’s the current save code:
func save_to_file(file_dir:String):
var serial_dictionary:FileAccess = FileAccess.open(file_dir, FileAccess.WRITE)
#save_game.store_var(valid_words)
var valid_words_pack = PackedScene.new()
valid_words_pack.pack($ValidWords)
var result = ResourceSaver.save(valid_words_pack,file_dir)
print(result, ": ", error_string(result)) #outputs 15: File unrecognized
assert(result == OK) #code stops here
serial_dictionary.close()
The packed scene consists of a single child node with this script:
extends Node
@export var valid_words:Dictionary = {}
Here’s the definition of a single Resource that would be instanced and saved in the above dictionary (310k of these):
class_name Word
extends Resource
@export var word : String :
set(value):
word = value.to_upper()
get:
return word
@export var def : Array[String] = [""]
@export var pos : Array[String] = [""]
func _init(new_word:String = "", new_pos:Array[String] = [""], new_def:Array[String] = [""]):
self.word = new_word
self.pos = new_pos
self.def = new_def
func _print():
print(self.word, ",", self.def, ",", self.pos)
func has_pos(pos_string:String):
return pos_string in pos
func has_def(def_string:String):
return def_string in def
func define():
return self.word + ": " + self.pos[0] + " " + self.def[0]