Godot Version
4.6
Question
So I’m once again back with an issue after transporting my code to 4.6. I have one game in 4.5, and currently working on another one in 4.6, and the code that I’ve been using in 4.5 does not work anymore.
This time the issue is with ResourceSaver. This is the code I’m using for saving the game:
extends Node
var persistent_data : PersistentData = load("res://Resources/PlayerResources/save_data.tres")
var save_path := "res://data.tres"
func _init():
load_game()
func save_game():
var current_scene = get_tree().current_scene
persistent_data.room_path = current_scene.scene_file_path
if current_scene is Room:
persistent_data.room_name = current_scene.room_name
ResourceSaver.save(persistent_data, save_path)
func load_game():
if FileAccess.file_exists(save_path):
persistent_data = ResourceLoader.load(save_path)
Where PersistentData is a custom resource with a bunch of subresources (the player stats, the player inventory (which got a bunch of subresources as well), etc.). Like I said, this code worked fine in 4.5, but in 4.6 the file does not seem to change. What could be the issue here? All of the values in the resources have @export, I’m sure that is not the problem.
Maybe unrelated but there are at least two severe issues with the code
- Don’t use
_init, you probably want _ready instead
- Don’t save user data to
res:// it will not work on exports; you probably want user://
- Using resources as save files opens you up to arbitrary code execution which could lead to you or your players being hacked.
Try fixing the first two, and then it’s probably worth re-writing your saves to JSON or ConfigFile
3 Likes
Thank you for your reply! I followed your first two recommendations, and it did not fix the issue. I’m aware of the third point, however I’d still like to use to resources, at least for the testing phase. Besides, isn’t it still strange that it will not work?
1 Like
I did not have that problem switching to 4.6, so I’d say it’s something in the way you’re doing it that relies on either something deprecated, or (more likely) a bug that was fixed. We can probably help you, but you haven’t given us much to work with.
1 Like
Thanks for the reply! So apparently I was facing the same problem in 4.5, and the way I fixed it then was to make the Player resource and the Inventory resource unique. However, I cannot do the same in the editor in 4.6 for some reason, so the solution I went with was to make said resources unique through code, aka using duplicate(true). So now everything works! Thanks for your reply anyway.
1 Like