Godot Version
Godot 4.5.1.stable.official
Question
I’ve created my own chunk-saving/loading system based on pre-determined` chunks saved as individual scenes, which load in a 3x3 square around (0,0) at runtime. As the player moves, the chunk manager unloads and reloads chunks, ensuring a 3x3 square of chunks surrounding them is always instanced. When a chunk is unloaded, it’s saved to disk as a .res file. When a chunk is loaded for the first time, it’s loaded from the scene, but any subsequent loads are from the disk. The issue I’m having is that when (0,0) is unloaded, the unload function indicates that it’s being saved, and the .res file exists; however, when it’s loaded again, it’s not there.
Node structure for test scene:
Basic Node Structure for each chunk, though the save load system should be able toaccount for any number of children:
Relevant functions:
func load_chunks():
for dx in range(-1, 2): # -1, 0, 1
for dy in range(-1, 2):
var chunk_x = PLAYER_CHUNK.x + dx
var chunk_y = PLAYER_CHUNK.y + dy
var coords = Vector2i(chunk_x, chunk_y)
# Construct save file path
var save_path = GlobalData.global_save_path + "chunk_" + str(coords.x) + "_" + str(coords.y) + ".res"
var chunk_instance = null
# First try to load from JSON if it exists
if FileAccess.file_exists(save_path):
#print('loading from disk: ', coords)
var packed_scene = load(save_path) # returns PackedScene
if packed_scene:
chunk_instance = packed_scene.instantiate()
#else:
# print("Failed to load saved chunk:", save_path)
#else:
#print('file does not exist: ', save_path)
# If no saved JSON, load from scene
if chunk_instance == null:
#print('loading from scene')
var scene_path = "res://test_chunk(" + str(coords.x) + "," + str(coords.y) + ").tscn"
var chunk_scene = load(scene_path)
if chunk_scene:
chunk_instance = chunk_scene.instantiate()
#else:
#print("Failed to load chunk scene:", scene_path)
# Add to scene if we successfully loaded or instantiated it
if chunk_instance:
$"Chunks".add_child(chunk_instance)
func save_node_to_disk(chunk):
var save_path = GlobalData.global_save_path
var coords = Vector2i(0, 0)
if "," in chunk.name:
coords.x = int(chunk.name.get_slice(",", 0))
coords.y = int(chunk.name.get_slice(",", 1))
var save_location = save_path + "chunk_" + str(coords.x) + "_" + str(coords.y) + ".res"
# Ensure children have correct owner for packing
for child in chunk.get_children():
child.owner = chunk
var packed = PackedScene.new()
var err = packed.pack(chunk) # returns an int error code
if err != OK:
#print("Failed to pack chunk:", err)
return
ResourceSaver.save(packed, save_location)
#print("Saved chunk as PackedScene:", save_location)



