Skip to content

Commit

Permalink
Merge pull request #52 from her001/savinggames
Browse files Browse the repository at this point in the history
Added links, clarification, consistency to "Saving games"
  • Loading branch information
akien-mga committed Mar 3, 2016
2 parents 89e044a + 4d6d203 commit f98e94d
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions tutorials/engine/saving_games.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ Serializing

The next step is to serialize the data. This makes it much easier to
read and store to disk. In this case, we're assuming each member of
group Persist is an instanced node and thus has a file path. GDScript
has helper functions for this such as dictionary.to_json() and
dictionary.parse_json() so we will use a dictionary. Our node needs to
contain a save function that returns this data. The save function will
look like this:
group Persist is an instanced node and thus has a path. GDScript
has helper functions for this, such as :ref:`Dictionary.to_json()
<class_Dictionary_to_json>` and :ref:`Dictionary.parse_json()
<class_Dictionary_parse_json>`, so we will use a dictionary. Our node needs to
contain a save function that returns this data. The save function will look
like this:

::

Expand Down Expand Up @@ -101,8 +102,8 @@ way to pull the data out of the file as well.
Game saved! Loading is fairly simple as well. For that we'll read each
line, use parse_json() to read it back to a dict, and then iterate over
the dict to read our values. But we'll need to first create the object
and we can use filename and parent to achieve that. Here is our load
function:
and we can use the filename and parent values to achieve that. Here is our
load function:

::

Expand All @@ -113,7 +114,7 @@ function:
return #Error! We don't have a save to load

# We need to revert the game state so we're not cloning objects during loading. This will vary wildly depending on the needs of a project, so take care with this step.
#For our example, we will accomplish this by deleting savable objects.
# For our example, we will accomplish this by deleting savable objects.
var savenodes = get_tree().get_nodes_in_group("Persist")
for i in savenodes:
i.queue_free()
Expand All @@ -123,7 +124,7 @@ function:
savegame.open("user://Invasionsave.save", File.READ)
while (!savegame.eof_reached()):
currentline.parse_json(savegame.get_line())
#First we need to create the object and add it to the tree and set its position.
# First we need to create the object and add it to the tree and set its position.
var newobject = load(currentline["filename"]).instance()
get_node(currentline["parent"]).add_child(newobject)
newobject.set_pos(Vector2(currentline["posx"],currentline["posy"]))
Expand Down

0 comments on commit f98e94d

Please sign in to comment.