saving_games.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. .. _doc_saving_games:
  2. Saving games
  3. ============
  4. Introduction
  5. ------------
  6. Save games can be complicated. For example, it may be desirable
  7. to store information from multiple objects across multiple levels.
  8. Advanced save game systems should allow for additional information about
  9. an arbitrary number of objects. This will allow the save function to
  10. scale as the game grows more complex.
  11. .. note::
  12. If you're looking to save user configuration, you can use the
  13. :ref:`class_ConfigFile` class for this purpose.
  14. Identify persistent objects
  15. ---------------------------
  16. Firstly, we should identify what objects we want to keep between game
  17. sessions and what information we want to keep from those objects. For
  18. this tutorial, we will use groups to mark and handle objects to be saved,
  19. but other methods are certainly possible.
  20. We will start by adding objects we wish to save to the "Persist" group. We can
  21. do this through either the GUI or script. Let's add the relevant nodes using the
  22. GUI:
  23. .. image:: img/groups.png
  24. Once this is done, when we need to save the game, we can get all objects
  25. to save them and then tell them all to save with this script:
  26. .. tabs::
  27. .. code-tab:: gdscript GDScript
  28. var save_nodes = get_tree().get_nodes_in_group("Persist")
  29. for i in save_nodes:
  30. # Now, we can call our save function on each node.
  31. .. code-tab:: csharp
  32. var saveNodes = GetTree().GetNodesInGroup("Persist");
  33. foreach (Node saveNode in saveNodes)
  34. {
  35. // Now, we can call our save function on each node.
  36. }
  37. Serializing
  38. -----------
  39. The next step is to serialize the data. This makes it much easier to
  40. read from and store to disk. In this case, we're assuming each member of
  41. group Persist is an instanced node and thus has a path. GDScript
  42. has helper class :ref:`JSON<class_json>` to convert between dictionary and string,
  43. Our node needs to contain a save function that returns this data.
  44. The save function will look like this:
  45. .. tabs::
  46. .. code-tab:: gdscript GDScript
  47. func save():
  48. var save_dict = {
  49. "filename" : get_filename(),
  50. "parent" : get_parent().get_path(),
  51. "pos_x" : position.x, # Vector2 is not supported by JSON
  52. "pos_y" : position.y,
  53. "attack" : attack,
  54. "defense" : defense,
  55. "current_health" : current_health,
  56. "max_health" : max_health,
  57. "damage" : damage,
  58. "regen" : regen,
  59. "experience" : experience,
  60. "tnl" : tnl,
  61. "level" : level,
  62. "attack_growth" : attack_growth,
  63. "defense_growth" : defense_growth,
  64. "health_growth" : health_growth,
  65. "is_alive" : is_alive,
  66. "last_attack" : last_attack
  67. }
  68. return save_dict
  69. .. code-tab:: csharp
  70. public Godot.Collections.Dictionary<string, object> Save()
  71. {
  72. return new Godot.Collections.Dictionary<string, object>()
  73. {
  74. { "Filename", GetFilename() },
  75. { "Parent", GetParent().GetPath() },
  76. { "PosX", Position.x }, // Vector2 is not supported by JSON
  77. { "PosY", Position.y },
  78. { "Attack", Attack },
  79. { "Defense", Defense },
  80. { "CurrentHealth", CurrentHealth },
  81. { "MaxHealth", MaxHealth },
  82. { "Damage", Damage },
  83. { "Regen", Regen },
  84. { "Experience", Experience },
  85. { "Tnl", Tnl },
  86. { "Level", Level },
  87. { "AttackGrowth", AttackGrowth },
  88. { "DefenseGrowth", DefenseGrowth },
  89. { "HealthGrowth", HealthGrowth },
  90. { "IsAlive", IsAlive },
  91. { "LastAttack", LastAttack }
  92. };
  93. }
  94. This gives us a dictionary with the style
  95. ``{ "variable_name":value_of_variable }``, which will be useful when
  96. loading.
  97. Saving and reading data
  98. -----------------------
  99. As covered in the :ref:`doc_filesystem` tutorial, we'll need to open a file
  100. so we can write to it or read from it. Now that we have a way to
  101. call our groups and get their relevant data, let's use the class :ref:`JSON<class_json>` to
  102. convert it into an easily stored string and store them in a file. Doing
  103. it this way ensures that each line is its own object, so we have an easy
  104. way to pull the data out of the file as well.
  105. .. tabs::
  106. .. code-tab:: gdscript GDScript
  107. # Note: This can be called from anywhere inside the tree. This function is
  108. # path independent.
  109. # Go through everything in the persist category and ask them to return a
  110. # dict of relevant variables.
  111. func save_game():
  112. var save_game = File.new()
  113. save_game.open("user://savegame.save", File.WRITE)
  114. var save_nodes = get_tree().get_nodes_in_group("Persist")
  115. for node in save_nodes:
  116. # Check the node is an instanced scene so it can be instanced again during load.
  117. if node.filename.empty():
  118. print("persistent node '%s' is not an instanced scene, skipped" % node.name)
  119. continue
  120. # Check the node has a save function.
  121. if !node.has_method("save"):
  122. print("persistent node '%s' is missing a save() function, skipped" % node.name)
  123. continue
  124. # Call the node's save function.
  125. var node_data = node.call("save")
  126. # JSON provides a static method to serialized JSON string
  127. var json_string = JSON.stringify(node_data)
  128. # Store the save dictionary as a new line in the save file.
  129. save_game.store_line(json_string)
  130. save_game.close()
  131. .. code-tab:: csharp
  132. // Note: This can be called from anywhere inside the tree. This function is
  133. // path independent.
  134. // Go through everything in the persist category and ask them to return a
  135. // dict of relevant variables.
  136. public void SaveGame()
  137. {
  138. var saveGame = new File();
  139. saveGame.Open("user://savegame.save", (int)File.ModeFlags.Write);
  140. var saveNodes = GetTree().GetNodesInGroup("Persist");
  141. foreach (Node saveNode in saveNodes)
  142. {
  143. // Check the node is an instanced scene so it can be instanced again during load.
  144. if (saveNode.Filename.Empty())
  145. {
  146. GD.Print(String.Format("persistent node '{0}' is not an instanced scene, skipped", saveNode.Name));
  147. continue;
  148. }
  149. // Check the node has a save function.
  150. if (!saveNode.HasMethod("Save"))
  151. {
  152. GD.Print(String.Format("persistent node '{0}' is missing a Save() function, skipped", saveNode.Name));
  153. continue;
  154. }
  155. // Call the node's save function.
  156. var nodeData = saveNode.Call("Save");
  157. // Store the save dictionary as a new line in the save file.
  158. saveGame.StoreLine(JSON.Print(nodeData));
  159. }
  160. saveGame.Close();
  161. }
  162. Game saved! Now, to load, we'll read each
  163. line. Use the :ref:`parse<class_JSON_method_parse>` method to read the
  164. JSON string back to a dictionary, and then iterate over
  165. the dict to read our values. But we'll need to first create the object
  166. and we can use the filename and parent values to achieve that. Here is our
  167. load function:
  168. .. tabs::
  169. .. code-tab:: gdscript GDScript
  170. # Note: This can be called from anywhere inside the tree. This function
  171. # is path independent.
  172. func load_game():
  173. var save_game = File.new()
  174. if not save_game.file_exists("user://savegame.save"):
  175. return # Error! We don't have a save to load.
  176. # We need to revert the game state so we're not cloning objects
  177. # during loading. This will vary wildly depending on the needs of a
  178. # project, so take care with this step.
  179. # For our example, we will accomplish this by deleting saveable objects.
  180. var save_nodes = get_tree().get_nodes_in_group("Persist")
  181. for i in save_nodes:
  182. i.queue_free()
  183. # Load the file line by line and process that dictionary to restore
  184. # the object it represents.
  185. save_game.open("user://savegame.save", File.READ)
  186. while save_game.get_position() < save_game.get_len():
  187. # Creates the helper class to interact with JSON
  188. var json = JSON.new()
  189. # Check if there is any error while parsing the JSON string, skip in case of failure
  190. var parse_result = json.parse(json_string)
  191. if not parse_result == OK:
  192. print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
  193. continue
  194. # Get the data from the JSON object
  195. var node_data = json.get_data()
  196. # Firstly, we need to create the object and add it to the tree and set its position.
  197. var new_object = load(node_data["filename"]).instantiate()
  198. get_node(node_data["parent"]).add_child(new_object)
  199. new_object.position = Vector2(node_data["pos_x"], node_data["pos_y"])
  200. # Now we set the remaining variables.
  201. for i in node_data.keys():
  202. if i == "filename" or i == "parent" or i == "pos_x" or i == "pos_y":
  203. continue
  204. new_object.set(i, node_data[i])
  205. save_game.close()
  206. .. code-tab:: csharp
  207. // Note: This can be called from anywhere inside the tree. This function is
  208. // path independent.
  209. public void LoadGame()
  210. {
  211. var saveGame = new File();
  212. if (!saveGame.FileExists("user://savegame.save"))
  213. return; // Error! We don't have a save to load.
  214. // We need to revert the game state so we're not cloning objects during loading.
  215. // This will vary wildly depending on the needs of a project, so take care with
  216. // this step.
  217. // For our example, we will accomplish this by deleting saveable objects.
  218. var saveNodes = GetTree().GetNodesInGroup("Persist");
  219. foreach (Node saveNode in saveNodes)
  220. saveNode.QueueFree();
  221. // Load the file line by line and process that dictionary to restore the object
  222. // it represents.
  223. saveGame.Open("user://savegame.save", (int)File.ModeFlags.Read);
  224. while (saveGame.GetPosition() < saveGame.GetLen())
  225. {
  226. // Get the saved dictionary from the next line in the save file
  227. var nodeData = new Godot.Collections.Dictionary<string, object>((Godot.Collections.Dictionary)JSON.Parse(saveGame.GetLine()).Result);
  228. // Firstly, we need to create the object and add it to the tree and set its position.
  229. var newObjectScene = (PackedScene)ResourceLoader.Load(nodeData["Filename"].ToString());
  230. var newObject = (Node)newObjectScene.Instantiate();
  231. GetNode(nodeData["Parent"].ToString()).AddChild(newObject);
  232. newObject.Set("Position", new Vector2((float)nodeData["PosX"], (float)nodeData["PosY"]));
  233. // Now we set the remaining variables.
  234. foreach (KeyValuePair<string, object> entry in nodeData)
  235. {
  236. string key = entry.Key.ToString();
  237. if (key == "Filename" || key == "Parent" || key == "PosX" || key == "PosY")
  238. continue;
  239. newObject.Set(key, entry.Value);
  240. }
  241. }
  242. saveGame.Close();
  243. }
  244. Now we can save and load an arbitrary number of objects laid out
  245. almost anywhere across the scene tree! Each object can store different
  246. data depending on what it needs to save.
  247. Some notes
  248. ----------
  249. We have glossed over setting up the game state for loading. It's ultimately up
  250. to the project creator where much of this logic goes.
  251. This is often complicated and will need to be heavily
  252. customized based on the needs of the individual project.
  253. Additionally, our implementation assumes no Persist objects are children of other
  254. Persist objects. Otherwise, invalid paths would be created. To
  255. accommodate nested Persist objects, consider saving objects in stages.
  256. Load parent objects first so they are available for the :ref:`add_child()
  257. <class_node_method_add_child>`
  258. call when child objects are loaded. You will also need a way to link
  259. children to parents as the :ref:`NodePath
  260. <class_nodepath>` will likely be invalid.
  261. JSON vs binary serialization
  262. ----------------------------
  263. For simple game state, JSON may work and it generates human-readable files that are easy to debug.
  264. But JSON has many limitations. If you need to store more complex game state or
  265. a lot of it, :ref:`binary serialization<doc_binary_serialization_api>`
  266. may be a better approach.
  267. JSON limitations
  268. ~~~~~~~~~~~~~~~~
  269. Here are some important gotchas to know about when using JSON.
  270. * **Filesize:**
  271. JSON stores data in text format, which is much larger than binary formats.
  272. * **Data types:**
  273. JSON only offers a limited set of data types. If you have data types
  274. that JSON doesn't have, you will need to translate your data to and
  275. from types that JSON can handle. For example, some important types that JSON
  276. can't parse are: ``Vector2``, ``Vector3``, ``Color``, ``Rect2``, and ``Quaternion``.
  277. * **Custom logic needed for encoding/decoding:**
  278. If you have any custom classes that you want to store with JSON, you will
  279. need to write your own logic for encoding and decoding those classes.
  280. Binary serialization
  281. ~~~~~~~~~~~~~~~~~~~~
  282. :ref:`Binary serialization<doc_binary_serialization_api>` is an alternative
  283. approach for storing game state, and you can use it with the functions
  284. ``get_var`` and ``store_var`` of :ref:`class_FileAccess`.
  285. * Binary serialization should produce smaller files than JSON.
  286. * Binary serialization can handle most common data types.
  287. * Binary serialization requires less custom logic for encoding and decoding
  288. custom classes.
  289. Note that not all properties are included. Only properties that are configured
  290. with the :ref:`PROPERTY_USAGE_STORAGE<class_@GlobalScope_constant_PROPERTY_USAGE_STORAGE>`
  291. flag set will be serialized. You can add a new usage flag to a property by overriding the
  292. :ref:`_get_property_list<class_Object_method__get_property_list>`
  293. method in your class. You can also check how property usage is configured by
  294. calling ``Object._get_property_list``.
  295. See :ref:`PropertyUsageFlags<enum_@GlobalScope_PropertyUsageFlags>` for the
  296. possible usage flags.