Godot Version
4.2
Question
I have a game with loads of scenes and in each scene I have monsters that I want to be permanent. So if the player leaves and re-enters the level scene the monsters have the same amount of health, the same position and if they were defeated I want to display the corpse.
My issue with that is that I can’t seem to figure out how to store that data globally. The main issue I have is that a change in the scene the monster scenes get deleted so that when I store the monster instance in a singleton it gets deleted as well. I also tried to store the instance_id but that one also changes everytime I reload the scene. Is there a good tutorial or system to keep track of this?
Pretty sure you can re-pack the scene as user data, that way everything gets saved if you try to load the user’s scene first. The player may need to be a special case to exclude.
# saving
var user_scene := PackedScene.new()
var result := user_scene.pack(map)
if result == OK:
result = ResourceSaver.save(user_scene, "user://%s" % map_name)
if result != OK:
push_error("Couldn't save packed map scene for '%s'!" % map_name)
# loading
var scene: PackedScene
if FileAccess.file_exists("user://%s" % map_name):
scene = load("user://%s" % map_name)
else:
scene = load("res://%s" % map_name)
var instance = scene.instantiate()
add_child(instance)
Is depends of type and size of game, you can remove from parent and keep them
Do you know if this would save a node’s position like saving it in the editor or do you have to export the properties you want serialized?
When saving resources I know they must be exported properties, I am not sure for packed scenes; I am pretty sure it will save any altered property if it is packed.