C

Godot 4 doesn't have setget anymore so how am I suppose to set up the save and load system?

CotTan

I have a save data system is giving me all kinds of problem for 2 days already, I followed the course tutorial first then also checked the GDQuest youtube version, but both of them is godot 3, Godot 4 nolonger have the setget function so I don't know if that's the part that's causing me problem.

as showend below are the codes, the process is I spawn the rooms first, then spawn the player, then load the data in, weird thing is , when the room first spawned(or I first run the game with F5) it will save and load just fine, i can even open up the tres file and modify the numbers then run the game again it will load perfectly fine, but once in the game, I can only save, but not load with the button I set, can anyone give me some hint on what's possible wrong?

another side issue is alot things in the set function are supposed to be in the ready function(not in the image I showed), such as connecting signals, if I ever needs to connect a singal to the player stats, with this system I had to put that connecting code inside the set_data function, other wise even the stats onready var is right there on top it will still say stats is null, but again because its in the set_data function, everytime I try to load the file it will run the code again to connect the signal that's already connected and throw me bunch of error. 

pls help~

updated a last extra screenshot, the new data is my first manual saving after start the game, the 1,1,1 is my player stats at lvl 1, the first resource i print out is my first manual load, it actually did load it for some reason, but it throw me a bunch of error because I have signal connections in the set_data or set_inventory function, then the second time I load it doesn't load the save file anymore, it just load a different thing I don't know where it comes from, player stats shows 222 rather than the saved 111, if I level up again and load it will become 333, I opened up the tres while the game is running and made sure is not auto saving, the tres file still shows 111.

024-02-06 213557.jpg

024-02-06 213512.jpg

024-02-06 010626.jpg

024-02-06 214258.jpg

024-02-06 213530.jpg

024-02-07 015445.jpg

  • Nathan Lovato replied

    Godot 4 does have setters and getters, the syntax just changed slightly. Here's how you can convert your code to use the new syntax:

    # Old syntax
    var my_var := 0 setget set_my_var
    func set_my_var(new_value: int) -> void:
        my_var = new_value

    # New syntax
    var my_var := 0: set = set_my_var
    func set_my_var(new_value: int) -> void:
        my_var = new_value

    We made a longer video on our channel detailing the feature a bit more and the two syntaxes Godot 4 supports: https://www.youtube.com/watch?v=PDISK_JPPQE

  • C
    CotTan replied

    thanks, it seems like I am using the new syntax right, in this case I don't know what's causing my button to not load anymore :(

  • Nathan Lovato replied

    Could you please compress your Godot project as a ZIP archive, then upload it somewhere, and share the download link with me?

    You can upload the ZIP file to a platform like Google Drive, We Transfer, or any file upload platform that you usually use. Then, from that platform, you should have an option to share a download link. You can copy and paste that download link in a reply here.

    I'll need that to get more context and figure out the source of the issue.

  • C
    CotTan replied

    https://drive.google.com/file/d/19C1e4Gyi2C1FzvNOPRZm7IVQw7GykVs0/view?usp=sharing

    here is the link, pls also take a look at the inventory, there are currently some connections that I had to put into the set function which I don't think is correct but I can't figure out any other way to do this, originally they were in the ready function but now with save load system the loaded data that needs to be connected returns null.

    thank you !

  • Nathan Lovato replied

    Thanks for the link! The file shared is private so I can't download it. I just requested access. I'll check it out when I have access to it.

  • C
    CotTan replied

    you got it now :) , by the way the test scene is the current main scene

  • Nathan Lovato replied

    First of all, that's a really impressive project you have there!

    "another side issue is alot things in the set function are supposed to be in the ready function(not in the image I showed), such as connecting signals, if I ever needs to connect a singal to the player stats, with this system I had to put that connecting code inside the set_data function, other wise even the stats onready var is right there on top it will still say stats is null,"

    Your onready save_game variable has no value assigned, so it will be null by default. Adding the onready annotation doesn't make a difference here.

    So the error you got when trying to connect signals in the ready function is likely because probably the save game was not loaded before trying to connect to its signals as the value was null: There was nothing to connect to. You need to first load the save game and store the loaded resource into the variable before you can connect to its signals.

    Note that the other issue you mention about errors with signals already connected: This indicates that Godot was caching the resource and not loading it from scratch, giving you the same object each time, so your general issue is that Godot isn't giving you a fresh resource object instance.

    Seeing how the resource behaves, I think that, each time you save, you'll need to duplicate your save game resource and call Resource.take_over_path() on it to force it to replace and overwrite the previous save game in the cache.

    And seeing this, I think in the remake of the course we'll show another approach than resources, because that's a bit confusing. I see that the ResourceLoader.load() function (not GDScript's load() function) has a parameter called cache_mode and undocumented options. I was expecting one of them to force the save game to properly reload, but it does not so far in my tests. So, I would recommend checking out the ConfigFile class in Godot 4 instead of using resources for savegames, for now. Alternatively, you could try to duplicate the resource and use ResourceLoader.has_cached() to ensure that the savegame resource (and any of its sub-resources) are not being cached by Godot, preventing a reload from the disk. But I would go with another approach like ConfigFile now.

  • C
    CotTan replied

    thanks. the advatage of using resource to save is all the data changing was already done as you play, when save and load all it did was swapping out the entire resource sheet, no need to write out individual stats that needs to be changed, the config route seems very similiar to the json route listing out every single thing that needs to be save and loaded. every tutorial I can find about config don't take account of resources, they just load and set the player script to certain things, but all my player and inventory data were build around resources, especially inventory, I may end up with 100 slot, so I am stucked not sure how to use config to edit the resource file, would you mind make a short video  just to show the steps of how to edit the data resource file with config method?

  • C
    CotTan replied

    actually I think kind of figured it out, give me some time to test it and post it here so you can take a look, I think I did it in a quite complex way, although it may work but not sure if its the proper way of achieving it

  • C
    CotTan replied

    024-02-12 225545.jpg

    so this is how I did the save with conif method, very ... complex and inconvenient compare to the resource route where all I had to do swapping out the resource sheet with 1 line,  but this is shortest I can get already, I hope I did it in the right way still.

  • Nathan Lovato replied

    Great job! It sure is less convenient than just swapping the resource, but at the same time, you were facing caching issues with resources, and hopefully with this you'll be able to progress.

    There's no right or wrong way to do this: the config file format gives you a lot of flexibility when storing and loading data. As long as it works for you, it's perfect!

    I'm taking note to get a hold of a Godot core dev who knows about the resource part of the codebase and see what's planned in this area for the godot 4 remake of the course. I think going with configfile is the safest option now teaching-wise. But of course, like you, I'd love it if we could make resources just work.