How to Easily Save Games with Resources in Godot (demo and guide)

Поділитися
Вставка
  • Опубліковано 28 чер 2022
  • This approach has a risk for players as resources can run arbitrary code. New video explaining that: • I was COMPLETELY WRONG...
    Learn to Code from Zero, With Godot: gdquest.mavenseed.com/courses...
    Godot Node Essentials (ultimate cookbook): gdquest.mavenseed.com/courses...
    ---
    📚 THE LINKS
    Open-source save demo project: github.com/GDQuest/godot-demo...
    ---
    👥 CREDITS
    Video edited by Nesi: / nesiawesomeness
    ---
    💌 JOIN US
    Come chat on Discord: / discord
    Follow us on Twitter: / nathangdquest
    ---
    📝 CC-BY LICENSE
    This video is licensed under the CC-By 4.0 license: creativecommons.org/licenses/...
    You can attribute it to "CC-By 4.0 - GDQuest and contributors - www.gdquest.com/"

КОМЕНТАРІ • 61

  • @Gdquest
    @Gdquest  2 роки тому +46

    A user raised the fact that resources can run arbitrary code and that, if a player decides to download a save file from an untrusted source, it could contain malicious code. It's true, just like any game mod or Godot add-on could contain malicious code, technically. You can protect against that when using resources though. You'll find the details here: github.com/GDQuest/godot-demos-2022/tree/main/save-game#security-of-resources
    Our teammate already worked on a fix for the resource bug presented in the video ( github.com/godotengine/godot/pull/62408 ). We're waiting for review but hopefully, we can get this fixed for Godot 3.5.1. Then it'll only take one line to reload savegames with nested sub-resources while the game's running (ResourceSaver.load(...)).
    If you like our latest videos and open-source demos, please check out our courses! Our ability to make Free stuff depends entirely on our sales. We're sponsored by the community only - you.

    • @Jose.Eduardo.C
      @Jose.Eduardo.C Рік тому +1

      I was learning this topic yesterday, how confused I was! The current latest documentation is pretty confusing too. But after a while I discovered that "File" has been discontinued and now "FileAccess" is used, and the thing about running code has been resolved (apparently, im not really an expert on the security of things) ! The method now has a boolean to check if you want to allow incoming code or not

  • @rienafaire9286
    @rienafaire9286 2 роки тому +11

    Yes , I HAVE BEEN WAITING FOR THIS , thank you so much
    I did a save system on my own and it was a spaghetti mess that doesn't work half the time .
    It's good to have a system by people who know what they're doing.

  • @Ryan-lp3qw
    @Ryan-lp3qw 2 роки тому

    I love this save system, thank you so much for this tutorial! I understand pretty much all of it, but I'm having trouble wrapping my mind how take_over_path() is enabling users to save a SaveGame resource at the path. I would think that because you converted the data at the SAVE_GAME_PATH to a text only (without the nested resources) that future saves would either overwrite the data with nested resources again, or it wouldn't save the data correctly. But your fix only gets called the second time load_savegame() is called, which means it worked for all future saves. Kudos to you guys for figuring out the fix. Thanks again

  • @dorianvedel1910
    @dorianvedel1910 2 роки тому

    This video format is really awesome !

  • @antimari22
    @antimari22 2 роки тому +10

    I found bug. if you push load button multiple times (without change anything). inventory and player stats no more reloaded. only player position can load.

  • @Lucrecious
    @Lucrecious 2 роки тому +23

    I love this idea for saving games and I'll probably use it myself for single-release games.
    One big issue with this though is that it doesn't handle backwards compatibility very well. If you release your game and want to change or add a new field to the save resource, v1.0, it seems like you'd have to copy/paste the entirety of the resource script tree to make a v1.1 Then when you load, you'd need to check if you've loaded v1.0 or v1.1 of the save format (the resource).
    This file format versions are a problem that plagues all file io, but with this method it can be worse because (1) you're coupling code text with File IO text, which means changing the name of a variable affects backwards compatibility (annoying to maintain after release) and (2) any time you do need to update the format, you'll need to keep the previous versions of the resources that have changed.
    Overall, I think it's much, much, much easier to not mess up File IO with this method though.
    Also, there are benefits to having to create an entirely new resource script to update the save format in that it makes versioning very clear albeit a little more verbose.
    It would be cool if export variables in 4.x for example had a way to set a "file IO name" and a "code node" separately - maybe through annotations. That way, refactoring an export variable identifier wouldn't require creating a new resource for backward compatibility.
    Anyways, thanks for the video. I enjoyed it and I can't believe I didn't think of this earlier!

    • @Gdquest
      @Gdquest  2 роки тому +12

      Adding new fields should be no problem, it just gets initialized to the scripts default value. We've done that several times on out end. Saved user data is also one of the cases where I'd try to apply the open-close principle: extend the resource, but don't modify existing code (to avoid issues).
      Then it's as you say, if you need to break compatibility, I'd recommend loading the old save, running it a conversion function and writing a new resource. Using nested resources one advantage is that you can version individual resources and convert just the part you need to break.
      Or the entire thing.
      Then if the game's really big and there's a ton to save, surely you'll need to evaluate your options. I don't have experience on large games, couldn't say how well resources would fare.
      At our size at gdquest though resources work great. We'll likely use them for saves in our upcoming open game

    • @Lucrecious
      @Lucrecious 2 роки тому +3

      @@Gdquest I agree.
      I think this method is substantially easier and harder to mess up.
      I just wanted to lay out cost and benefits is all.

  • @realMenta
    @realMenta 2 роки тому

    Thank you for the quality tutorial!!

  • @vidvad
    @vidvad 2 роки тому +3

    Thanks for the video. Very interesting!
    Would you recommend encrypting/decrypting the Resource file on save/load to prevent the user to modify its stats directly from the hard drive ? If so, that could be a very interesting follow up to this video :)

  • @Peebly
    @Peebly Рік тому +4

    I’ve been totally sold on resources and have been using them for the past few months while developing my game.
    Most games I’ve seen in examples and in tutorials use only one resource to pull stats from for one object.
    In my game (and in some other genres I can imagine) you will need to use a resource as a template and have that data passed to multiple instances of an object. In my case I’ve done this by using the duplicate() function on my resources.
    In this situation would a save system like this even work? It seems like I would need to dynamically create variables to store the new resource data.
    I feel there may be a better solution I have not been able to figure out yet. Let me know your thoughts!
    Thanks again for spreading the good word about resources!

  • @kiyu3229
    @kiyu3229 2 роки тому +1

    thanks for doing this video !!

  • @Gredran
    @Gredran 2 роки тому +2

    Amazing as always and yay for regular videos again!
    Question out of curiosity: is there a way using one or multiple of these methods to somehow trigger the state to be saved when a player sends out a bug report?
    Like so you can see the exact state the game was in for easy bug reproduction and solving?

    • @Gdquest
      @Gdquest  2 роки тому

      Actually yes, you have access to Godot's scene save and load mechanism. I think you can save the player's node tree with all the properties and send that. Get their scene by calling get_tree().current_scene, then create a PackedScene resource, call its pack() method, and save that to the disk. You'll get a tscn file.
      As long as it doesn´t rely on any files that aren´t in the project (mods etc.) you should be able to open it in Godot.
      I'm not sure how useful this would be for debugging, but this could be and it's possible.

    • @Gredran
      @Gredran 2 роки тому +1

      @@Gdquest i have seen a few AAA behind the scenes videos and recently a game someone made in Flash of all things where they had a turn based game that when a player made a bug report, the data of the exact game state was sent with the report, so the developer was able to copy and paste the output of code that was created(I think it was represented by a string that wasn’t actual code but just random letters and numbers, but idk if that matters or not) and when he pasted the output of the bug report, he was jumped to the guy’s exact turn vs the exact opponent and reproduce the bug right there.
      And then he was even able to delete a chunk of the code he had pasted in to jump back a turn but he was in the same exact game “seed” of the guy’s bug report so he was able to play into the next turn and actually better understand what was happening when he reproduced the bug in the following turn.
      It may be understandably complex to setup, but it does seem powerful to jump to an exact frame or a few before to be able to see exact bug conditions

  • @jSnakeChips
    @jSnakeChips Рік тому +7

    This seems like a good way to save things but with setget being different now and being 4.1 as well I feel like this tutorial need an update to run properly

    • @Micahtmusic
      @Micahtmusic 11 місяців тому +2

      yes please

    • @unknownjf
      @unknownjf 8 місяців тому +3

      i had problems with this, but i made the setget on my autoload functions and assign the requested variable as a variable from the resource file and assign this variable as the one who activated the setget
      semething like:
      resource
      var food: int = 3
      autoload script
      var food: int = 0 : set = food_setter
      var resource_file = "res://resource.tres"
      func food_setter(value):
      stuff to do
      more stuff to do
      resource_file.food = value
      kind of third-party-ing the thing

  • @zainsagar5833
    @zainsagar5833 Рік тому

    bro u are so awesome man so many videos and tutorial …with good explanation god bless u man and keep make more peace. Still my logic build has not developed can u share some tips …. like how u very able to build such good logic …..like how u know what's goes in what

  • @TG-Nexus
    @TG-Nexus 2 роки тому

    hey bro pls am facing a problem in godot and i guess u skipped it in your video how can i find out how to solve it :(

  • @pixelstudios51
    @pixelstudios51 2 роки тому +1

    pasted the load_savegame() function from demo to test and it errors for me? "a non void function must return a value at all possible paths"

    • @unknownjf
      @unknownjf 8 місяців тому

      idk if it's from godot update, but the test codes are being like:
      func headbang() -> stuff:
      if variable == 1:
      return something or function
      but can you see that outside the if there's nothing? you should add another return:
      func headbang() -> stuff:
      if variable == 1:
      return something or function
      return null

  • @jandro1565
    @jandro1565 6 місяців тому +2

    I am intrested in saving an entire scene, but there are to many variables and parametres. If Godot use resources to Load your scene, can I just save the scene itself?

    • @Gdquest
      @Gdquest  6 місяців тому +2

      Yes, you can. All the nodes that have to be saved must have an `owner` property. Nodes created through the editor will have it automatically. All properties that must be saved have to be `@export`ed.

  • @FlummoxTheMagnificent
    @FlummoxTheMagnificent Рік тому +1

    Can you make a video explaining how to save accounts/data to LootLocker?

  • @user-vj6pp6rz2z
    @user-vj6pp6rz2z 2 місяці тому

    Is it possible to save resources not as plain text files but as binary?

  • @givinsstaelkoudimba7211
    @givinsstaelkoudimba7211 Рік тому

    Is it possible to save a node? I want to save my player life bar node but it doesn't work... Even when I put the node into a variable it doesn't work... Can you help me please (I'm almost releasing my first beta version for the game I'm working on)

    • @unknownjf
      @unknownjf 8 місяців тому

      save a node in a file? actually no. but you can save the value of you life bar and reload it when you need

  • @frazziii
    @frazziii Рік тому

    Great video! Can you have a recursive resource, like a binary tree or a trie? And can you store it in binary format?

    • @Gdquest
      @Gdquest  Рік тому +2

      Yes and yes! If you click the link to the demo there's a text guide that shows that.

    • @frazziii
      @frazziii Рік тому +1

      @@Gdquest Thanks! Seems to be a bug in Godot 4 beta as well, but different, hopefully fixed in the upcomming beta releases

  • @frozenparrot
    @frozenparrot Рік тому +2

    let's say I want to have many players, each with different stats, how would I go about saving each of their stats?

    • @joshualozano3105
      @joshualozano3105 Рік тому

      I'm also interested in an answer for this, hopefully we get one

  • @user-ph6zu8wh6h
    @user-ph6zu8wh6h Рік тому

    can you make tutorial about saving tilemap (2d platformer world) like terraria? i dont know where to start again after generate the terrain. please i need to know this hehe :D

  • @MenicaFolden
    @MenicaFolden 2 роки тому +2

    Does this solution works for browser games?

    • @Gdquest
      @Gdquest  2 роки тому +1

      Yes, we use it for the app Learn GDScript. I´d just recommend checking the demo and text guide for notes about security.

  • @mars3142
    @mars3142 2 роки тому +1

    Should we encrypt the savegames?

    • @sslaxx
      @sslaxx 2 роки тому +1

      Up to you. Some games (Sword and Sandals 6, for one) do, others do not.

  • @linodev5540
    @linodev5540 2 роки тому +2

    What distro are you using? its soo clean

    • @Urakaroo
      @Urakaroo Рік тому +2

      It looks like Pop!_OS to me.

  • @MotiveStorm208
    @MotiveStorm208 2 роки тому +1

    Can you make a tutorial on chess with godot
    Please

  • @soran2290
    @soran2290 2 роки тому +1

    how to use saving by versions?

    • @Gdquest
      @Gdquest  2 роки тому

      We have an explanation of versioning in another comment. The way I do it typically is:
      - Have a variable named version in the savegame, an integer. I use that to know when/how to update a savegame resource.
      - Favor adding new saved fields (new exported variables) over renaming existing ones, as renaming them will cause issues during loading.
      - If at some point you want to completely change the save file's structure, you need to make a new script for it with your new structure and convert the old save into the new format.

  • @MaulSigma
    @MaulSigma 9 днів тому

    the set stats is function

  • @marcosmacias2087
    @marcosmacias2087 2 роки тому

    Hi good video godot You create video Game sabe for andriod please

  • @JD10NN3
    @JD10NN3 5 місяців тому

    What about cheating?

  • @Tenkite001
    @Tenkite001 7 місяців тому

    So complicated

  • @spenceabeen
    @spenceabeen 2 роки тому +1

    Gonk

  • @cprn.
    @cprn. 2 роки тому +14

    People calling this pseudo-isometric 2.5D view "top-down" make it very very troublesome to find true top-down assets, videos, tutorials, etc. Go and try.

    • @SirRichard94
      @SirRichard94 Рік тому +5

      It is top-down, the only difference is the visual of the sprites. Like zelda link to the past and minish cap. The grid is square and axis aligned.

    • @cprn.
      @cprn. Рік тому

      @@SirRichard94 The fact you're saying "the only difference is" means it's *not* top-down. Zelda games absolutely aren't top-down because they're in non-orthographic perspective. It's hard to characterize their style because it's a mix of unrealistic projections that go under the hypernym of 2.5D (or historically z-tilting but nowadays it means something else). If it was top-down, sprites would not be drawn from an angle but in a plan view. I challenge you to go and look for assets that are true top-down projections - you'll get what I'm talking about. What we see here is "almost top-down". Imagine the confusion if we were calling every non-alcoholic beverage just "beer".

    • @SirRichard94
      @SirRichard94 Рік тому +6

      @@cprn. its just visual. Just draw the sprite top down and itll work the same

    • @cprn.
      @cprn. Рік тому

      @@SirRichard94 Read the comments again.

    • @SirRichard94
      @SirRichard94 Рік тому +5

      @@cprn. but mechanically they work just the same. Ifk why you cant just translate it from one to another. Its like saying two cars are different because they have different paint.

  • @ohiasdxfcghbljokasdjhnfvaw4ehr
    @ohiasdxfcghbljokasdjhnfvaw4ehr 5 місяців тому

    too complicated