Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve C# code sample for saving nodes #10394

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions tutorials/io/saving_games.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ The save function will look like this:
};
}

// ISaveable.cs
// Define an interface to be used by the overarching Save() method
// which the Node you wish to persist will need to implement.
public interface ISaveable
{
public Godot.Collections.Dictionary<string, Variant> Save();
}
Comment on lines +121 to +127
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is clear enough, since we never explicitly show the Node implementing the interface. Also, comments in code can't be translated so that may make the C# code less accessible.



This gives us a dictionary with the style
``{ "variable_name":value_of_variable }``, which will be useful when
Expand Down Expand Up @@ -167,7 +175,7 @@ way to pull the data out of the file as well.

// Note: This can be called from anywhere inside the tree. This function is
// path independent.
// Go through everything in the persist category and ask them to return a
// Go through everything in the persist category that implements the ISaveable interface and ask them to return a
// dict of relevant variables.
public void SaveGame()
{
Expand All @@ -183,21 +191,24 @@ way to pull the data out of the file as well.
continue;
}

// Check the node has a save function.
if (!saveNode.HasMethod("Save"))
// Check the node implements the expected interface
if (saveNode is ISaveable saveableNode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could make the diff smaller by inverting the condition with is not and keeping the early continue as before.

Suggested change
if (saveNode is ISaveable saveableNode)
if (saveNode is not ISaveable saveableNode)

{
GD.Print($"persistent node '{saveNode.Name}' is missing a Save() function, skipped");
continue;
}
// Call the node's save function.
var nodeData = saveableNode.Save();

// Call the node's save function.
var nodeData = saveNode.Call("Save");
// Json provides a static method to serialized JSON string.
var jsonString = Json.Stringify(nodeData);

// Json provides a static method to serialized JSON string.
var jsonString = Json.Stringify(nodeData);
// Store the save dictionary as a new line in the save file.
saveFile.StoreLine(jsonString);
}
else
{
GD.Print($"persistent node '{saveNode.Name}' does not implement the ISaveable interface, skipped");
continue;
}

// Store the save dictionary as a new line in the save file.
saveFile.StoreLine(jsonString);
}
}

Expand Down