.. _doc_saving_games: Saving games ============ Introduction ------------ Save games can be complicated. For example, it may be desirable to store information from multiple objects across multiple levels. Advanced save game systems should allow for additional information about an arbitrary number of objects. This will allow the save function to scale as the game grows more complex. .. note:: If you're looking to save user configuration, you can use the `ConfigFile` class for this purpose. Identify persistent objects --------------------------- Firstly, we should identify what objects we want to keep between game sessions and what information we want to keep from those objects. For this tutorial, we will use groups to mark and handle objects to be saved, but other methods are certainly possible. We will start by adding objects we wish to save to the "Persist" group. We can do this through either the GUI or script. Let's add the relevant nodes using the GUI: ![](img/groups.png) Once this is done, when we need to save the game, we can get all objects to save them and then tell them all to save with this script: gdscript GDScript ``` var save_nodes = get_tree().get_nodes_in_group("Persist") for i in save_nodes: # Now, we can call our save function on each node. ``` Serializing ----------- The next step is to serialize the data. This makes it much easier to read from and store to disk. In this case, we're assuming each member of group Persist is an instanced node and thus has a path. GDScript has helper functions for this, such as `to_json()